开发者

LINQ query result in int type variable

开发者 https://www.devze.com 2023-02-19 09:00 出处:网络
I want the the LINQ query result in int type variable. i have a query this will always return the single int value. i want result sumthing like that.

I want the the LINQ query result in int type variable.

i have a query this will always return the single int value. i want result sumthing like that.

 int interlineId = from cSInter开发者_Python百科line in codeShareInterline_.AsEnumerable() 
                   where cSInterline.Field<int>("InterCodeId") == interCodeId[0] 
                   select cSInterline.Field<int>("PermitedPercent");

But it returning the error..


Your query is returning an IEnumerable<int>, with only one item in this case. So add Single or SingleOrDefault onto the end to return only that 1 item. If your query might return more than 1 item then use FirstOrDefault.

int interlineId = 
       (from cSInterline in codeShareInterline_.AsEnumerable() 
        where cSInterline.Field<int>("InterCodeId") == interCodeId[0] 
        select cSInterline.Field<int>("PermitedPercent")).SingleOrDefault();


Try this:

int interlineId = (from cSInterline in codeShareInterline_.AsEnumerable() 
              where cSInterline.Field<int>("InterCodeId") == interCodeId[0] 
              select cSInterline).Single().Field<int>("InterCodeId");


Try this (it should work):

 int? interlineId = (from cSInterline in codeShareInterline_.AsEnumerable() 
                   where cSInterline.Field<int>("InterCodeId") == interCodeId[0] 
                   select cSInterline.Field<int>("PermitedPercent")).FirstOrDefault();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号