我写了一个LINQ查询子句,但错误提升了。任何人都可以帮我解决。谢谢你提前:
list = (from cr in dt.AsEnumerable()
select new Cards
{
MemberNumber = cr.Field<string>("MemberNumber"),
if( cr.Field<int>("CardState")==0)
{
CardState ="aaa";
}
if( cr.Field<int>("CardState")==1)
{
CardState ="bbb";
}
if( cr.Field<int>("CardState")==2)
{
CardState ="ccc";
}
if( cr.Field<int>("CardState")==3)
{
CardState ="ddd";
}
if( cr.Field<int>("CardState")==4)
{
CardState ="eee";
}
}).ToList<Cards>();
答案 0 :(得分:3)
你不能在初始化程序中拥有这样的分支逻辑。你可以把它写成一系列三元运算符,但是:
list = (from cr in dt.AsEnumerable()
select new Cards
{
MemberNumber = cr.Field<string>("MemberNumber"),
CardState = ( cr.Field<int>("CardState")==0) ? "aaa" :
( cr.Field<int>("CardState")==1) ? "bbb" :
( cr.Field<int>("CardState")==2) ? "ccc" :
( cr.Field<int>("CardState")==3) ? "ddd" :
( cr.Field<int>("CardState")==4) ? "eee" :
( cr.Field<int>("CardState")==5) ? "bbb" : null
}).ToList<Cards>();
虽然更好的解决方案可能是将代码块分离为单独的函数并从初始化程序中调用它:
list = (from cr in dt.AsEnumerable()
select new Cards
{
MemberNumber = cr.Field<string>("MemberNumber"),
CardState = GetCardState( cr.Field<int>("CardState"))
}).ToList<Cards>();