c# - LINQ Group by & SUM to a LONG variable -


long  boq_val = (from boq_table in ctx.tbl_boq                  (boq_locationid == boq_table.location_id                         && boq_code_id == boq_table.workcode_id)                  group boq_table boq_table.workcode_id g                  select new                          {                             units_total = (long)g.sum(x => x.units)                          }); 

hi getting error as

"cannot implicitly convert type system.linq.iqueryable long"

please me need's units total (where above condition) long variable

the result of query iqueryable<long> while want boq_val long.

you first filter workcodeid, gives iqueryable<boq_table> , group boq's workcodeid. leaves iqueryable<igrouping<int, long>>, key workcode_id. each grouping select long, leaving iqueryable<long>. , both know there @ 1 result, because grouped on variable filtered on earlier, c# doesn't know that, have pick out 1 result.

what nicest way re-write you. if filter on boq_code_id where, no longer have group it, 1 way be

long  boq_val = (from boq_table in ctx.tbl_boq                  (boq_locationid == boq_table.location_id                      && boq_code_id == boq_table.workcode_id)                  ).sum(boq_table => (long)boq_table.units); 

personally full extension methods more (i think it's clearer way. seem minority though). going way give

long  boq_val = ctx.tbl_boq.where(boq_table =>                       (boq_locationid == boq_table.location_id &&                       boq_code_id == boq_table.workcode_id)                  ).sum(boq_table => (long)boq_table.units); 

if boq_table.units long, typecast nothing here. if it's int, might needed prevent overflow. if that's goal of cast, needs on inside of sum, otherwise cast overflown wrapped around int long, absolutely nothing.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -