'C# switch in lambda expression

Is it possible to have a switch in a lambda expression? If not, why? Resharper displays it as an error.



Solution 1:[1]

In a pure Expression (in .NET 3.5), the closest you can get is a compound conditional:

    Expression<Func<int, string>> func = x =>
        x == 1 ? "abc" : (
        x == 2 ? "def" : (
        x == 3 ? "ghi" :
                 "jkl")); /// yes, this is ugly as sin...

Not fun, especially when it gets complex. If you mean a lamda expression with a statement body (only for use with LINQ-to-Objects), then anything is legal inside the braces:

    Func<int, string> func = x => {
        switch (x){
            case 1:  return "abc";
            case 2:  return "def";
            case 3:  return "ghi";
            default: return "jkl";
        }
    };

Of course, you might be able to outsource the work; for example, LINQ-to-SQL allows you to map a scalar UDF (at the database) to a method on the data-context (that isn't actually used) - for example:

var qry = from cust in ctx.Customers
          select new {cust.Name, CustomerType = ctx.MapType(cust.TypeFlag) };

where MapType is a UDF that does the work at the db server.

Solution 2:[2]

Yes, it works, but you have to put your code in a block. Example:

private bool DoSomething(Func<string, bool> callback)
{
    return callback("FOO");
}

Then, to call it:

DoSomething(val =>
                {
                    switch (val)
                    {
                        case "Foo":
                            return true;

                        default:
                            return false;
                    }
                });

Solution 3:[3]

Hmm, I see no reason why this shouldn't work. Just be careful with the syntax you use

param => {
    // Nearly any code!
}

delegate (param) {
    // Nearly any code!
}

param => JustASingleExpression (No switches)

Solution 4:[4]

I checked it too :-)

[Test]
public void SwitchInLambda()
{
    TakeALambda(i => {
        switch (i)
        {
            case 2:
                return "Smurf";
            default:
                return "Gnurf";
        }
    });
}

public void TakeALambda(Func<int, string> func)
{
    System.Diagnostics.Debug.WriteLine(func(2));
}

Works just fine (outputs "Smurf")!

Solution 5:[5]

enter image description here

I just learn this:

                      (model) =>
                                {
                                    switch(model.SentInvoiceTypeId)
                                    {
                                        case 1:
                                            return "1 asdf";
                                        case 2:
                                            return "2 asdf";
                                        case 3:
                                            return "3 asdf ";
                                        case 4:
                                            return "4 asdf ";
                                        default:
                                            return "asdf";
                                    }
                                }

Just put between the "model" () and add your code in { }, remember to have a return.
I am not sure in which versions of C# will work, In this example is the C# 7.0

I hope this answer can help someone.

Solution 6:[6]

You can in a statement block lambda:

public static string CarBrandIdNotExist(AppLanguage language) => language switch
    {
        AppLanguage.English => "CarBrandId not exist",
        AppLanguage.Arabic => "CarBrandId not exist",
        AppLanguage.India => "CarBrandId not exist",
        AppLanguage.Filipino => "CarBrandId not exist",
        _ => "CarBrandId not exist",
    };

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Brian Genisio
Solution 3 Dario
Solution 4 Tor Haugen
Solution 5 Cristina Carrasco
Solution 6 reza.Nikmaram