C# switch in lambda expression

后端 未结 6 921
夕颜
夕颜 2021-02-04 04:04

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

6条回答
  •  深忆病人
    2021-02-04 05:06

    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.

提交回复
热议问题