c# - entity framework - how can I implement this SQL in the abbreviated EF linq -
can out c# code implement sql entity framework linq in abbreviated form? (e.g. have "." notation such xxx.where(... etc)
select pn.name, sum(u.amount) usages u, processnames pn pn.id == u.processnameid , u.datetime between '2010-01-08' , '2010-10-11' group pn.name
method-based query:
implement in lambda need leverage queryable.groupjoin:
var query = context.processnames .groupjoin(context.usages .where(u => u.datetime >= new datetime(2010, 1, 8) ) && u.datetime <= new datetime(2010, 10, 11), pn => pn.id, u => u.processnameid, (pn, usages) => new { name = pn.name, sum = usages.sum(u => u.amount) });
query expression:
, same query in query expression syntax:
var query = pn in context.processnames join u in context.usages .where(u => u.datetime >= new datetime(2010, 1, 8) ) && u.datetime <= new datetime(2010, 10, 11), on pn.id equals u.processnameid g select new { name = pn.name, sum = g.sum(u => u.amount) };
check generated sql:
verify these queries give desired sql command @ runtime can this:
string sqlcommand = ((objectquery)query).totracestring();
more examples:
examples on groupjoin, please take @ these:
method-based query syntax examples: join operators
query expression syntax examples: join operators
Comments
Post a Comment