C# 使用表达式Func返回值
我有下面的代码,其中有一个基于输入返回int的表达式。我得到的不是所有代码路径都返回值错误。有人能告诉我我在这里做错了什么语法吗C# 使用表达式Func返回值,c#,.net,C#,.net,我有下面的代码,其中有一个基于输入返回int的表达式。我得到的不是所有代码路径都返回值错误。有人能告诉我我在这里做错了什么语法吗 public static Expression<Func<BidCountry, int?>> GetWFMIDMapping { get { return bidcountry => {
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;
};
}
}
公共静态表达式GetWFMIDMapping
{
得到
{
返回国家=>
{
如果(bidcountry.WFMClockType==null){return null;}
else if(bidcountry.WFMDXModel==null | | bidcountry.WFMDXModel==false){return bidcountry.WFMClockType;}
else if(bidcountry.WFMDXModel==true)
{
开关(bidcountry.WFMClockType)
{
案例296:
返回265;
案例297:
返回266;
案例298:
返回267;
案例299:
返回268;
案例300:
返回269;
案例301:
返回270;
案例302:
返回271;
案例303:
返回272;
违约:
打破
}
}
否则返回bidcountry.WFMClockType;
};
}
}
感谢您使用下面的正确格式设置代码
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;
}
}
公共静态表达式GetWFMIDMapping
{
得到
{
返回国家=>
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:
无效的
}
}
假设您打算在非特殊情况下返回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;
}
}
公共静态表达式GetWFMIDMapping
{
get=>bidcountry=>{
if(bidcountry.WFMClockType==null)
返回null;
else if(bidcountry.WFMDXModel)
开关(bidcountry.WFMClockType){
案例296:
返回265;
案例297:
返回266;
案例298:
返回267;
案例299:
返回268;
案例300:
返回269;
案例301:
返回270;
案例302:
返回271;
案例303:
返回272;
违约:
打破
}
返回bidcountry.WFMClockType;
}
}
JIC为什么需要表达式?另外,带有语句体的lambda表达式无法转换为表达式树。将其用于某些Linq到SQL的内容。您的开关有一个默认值:break
从开关中掉出,然后从if中掉出,然后从lambda中掉出,并且从不返回值。请尝试。