17

是否可以在 lambda 表达式中进行切换?如果不是,为什么?Resharper 将其显示为错误。

4

6 回答 6

23

您可以在语句块 lambda 中:

Action<int> action = x =>
{
  switch(x)
  {
    case 0: Console.WriteLine("0"); break;
    default: Console.WriteLine("Not 0"); break;
  }
};

但是你不能在“单个表达式 lambda”中做到这一点,所以这是无效的:

// This won't work
Expression<Func<int, int>> action = x =>
  switch(x)
  {
    case 0: return 0;
    default: return x + 1;
  };

这意味着您不能在表达式树中使用 switch(至少由 C# 编译器生成;我相信 .NET 4.0 至少在库中支持它)。

于 2009-09-16T13:28:09.147 回答
12

在纯Expression(在 .NET 3.5 中)中,您可以获得的最接近的是复合条件:

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

不好玩,尤其是当它变得复杂时。如果您的意思是带有语句体的 lamda 表达式(仅用于 LINQ-to-Objects),那么大括号内的任何内容都是合法的:

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

当然,您也许可以将工作外包;例如,LINQ-to-SQL 允许您将标量 UDF(在数据库中)映射到数据上下文(实际未使用)上的方法 - 例如:

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

MapType在 db 服务器上执行工作的 UDF在哪里。

于 2009-09-16T13:41:10.130 回答
7

是的,它有效,但您必须将代码放在一个块中。例子:

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

然后,调用它:

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

                        default:
                            return false;
                    }
                });
于 2009-09-16T13:30:28.220 回答
2

嗯,我看不出为什么这不应该工作。请注意您使用的语法

param => {
    // Nearly any code!
}

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

param => JustASingleExpression (No switches)
于 2009-09-16T13:29:38.253 回答
2

我也检查了:-)

[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));
}

工作得很好(输出“蓝精灵”)!

于 2009-09-16T13:31:30.107 回答
0

在此处输入图像描述

我只是学习这个:

                      (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";
                                    }
                                }

只需放在“模型”()之间并在{}中添加您的代码,记得有一个返回。
我不确定哪些版本的 C# 可以工作,在这个例子中是 C# 7.0

我希望这个答案可以帮助某人。

于 2019-02-07T18:25:55.127 回答