简体   繁体   English

使用表达式函数返回值

[英]Using Expression Func to return value

I have the below code in which I have an expression which return an int based on the input.我有下面的代码,其中我有一个表达式,它根据输入返回一个 int。 I am getting a not all code path returns a value error.我得到一个并非所有代码路径都返回值错误。 Can someone please tell me what syntax am I doing wrong here.有人可以告诉我我在这里做错了什么语法。

public static Expression<Func<BidCountry, int?>> GetWFMIDMapping
        {
            get
            {
                return bidcountry =>
                {
                    if (bidcountry.WFMClockType == null) { return null; }
                    else if (bidcountry.WFMDXModel == null || bidcountry.WFMDXModel == false) { return bidcountry.WFMClockType; }
                    else if (bidcountry.WFMDXModel == true)
                    {
                        switch (bidcountry.WFMClockType)
                        {
                            case 296:
                                return 265;
                            case 297:
                                return 266;
                            case 298:
                                return 267;
                            case 299:
                                return 268;
                            case 300:
                                return 269;
                            case 301:
                                return 270;
                            case 302:
                                return 271;
                            case 303:
                                return 272;
                            default:
                                break;
                        }
                    }
                    else return bidcountry.WFMClockType;
                };
            }
        }

Thanks谢谢

Used the below to properly format the code使用以下内容正确格式化代码

   public static Expression<Func<BidCountry, int?>> GetWFMIDMapping
    {
        get
        {
            return bidcountry =>
            bidcountry.WFMClockType == null ? null :
           (bidcountry.WFMDXModel == null || bidcountry.WFMDXModel == false) ? bidcountry.WFMClockType :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 296) ? 265 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 297) ? 266 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 298) ? 267 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 299) ? 268 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 300) ? 269 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 301) ? 270 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 302) ? 271 :
           (bidcountry.WFMDXModel == true && bidcountry.WFMClockType == 303) ? (int?)272 :
           null;             
        }
    }

Assuming you meant to return WFMClockType when it wasn't one of the special cases, I would re-write like this:假设您打算在不是特殊情况之一时返回WFMClockType ,我会这样重写:

public static Expression<Func<BidCountry, int?>> GetWFMIDMapping
{
    get => bidcountry => {
        if (bidcountry.WFMClockType == null)
            return null;
        else if (bidcountry.WFMDXModel)
            switch (bidcountry.WFMClockType) {
                case 296:
                    return 265;
                case 297:
                    return 266;
                case 298:
                    return 267;
                case 299:
                    return 268;
                case 300:
                    return 269;
                case 301:
                    return 270;
                case 302:
                    return 271;
                case 303:
                    return 272;
                default:
                    break;
            }
        return bidcountry.WFMClockType;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM