Shivi
Shivi

Reputation: 1085

LINQ query result in int type variable

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 cSInterline in codeShareInterline_.AsEnumerable() 
                   where cSInterline.Field<int>("InterCodeId") == interCodeId[0] 
                   select cSInterline.Field<int>("PermitedPercent");

But it returning the error..

Upvotes: 0

Views: 3666

Answers (3)

Roman
Roman

Reputation: 276

Try this:

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

Upvotes: 2

Geoff Appleford
Geoff Appleford

Reputation: 18832

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();

Upvotes: 6

Brian Driscoll
Brian Driscoll

Reputation: 19635

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();

Upvotes: 1

Related Questions