c# - entity framework sum and average without grouping -


is there way in entity framework following simple query

sql query

select avg(totalsalary), avg(totalhours) context.employeedetails company = 1234 

in entity framework doing group in

employeedetails.where(c => c.company = 1234).groupby(c => c.company).select(x => new              {                 avgsalary = x.average(c => c.totalsalary),                  = x.average(c => c.totalhours)             }) 

is there correct way re-write above query avoid unnecessary group

is there issue using group? (which incidentally done groupby(c => c)) given you've used clause filter.

that said, cannot (as far know) written in linq (without generating 2 queries instead of one). if happy 2 queries, naturally like:

var query = context.mytable.where(x => x.myval == 1);  var queryselect = new {     avevalue1 = query.average(x => x.property1),     avevalue2 = query.average(x => x.property2) }; 

... expect, performs slower.

i therefore recommend use query have, albeit altering groupby or alternatively may better performance writing stored procedure , mapping sp context can say:

var query = mycontext.mystoredprocedure(companyid); 

(for example)! hope helps.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -