Entity structure - how can I implement this SQL in acronym EF linq

Can someone help with what C # code will use to implement this SQL as Entity Framework Linq in abbreviated form? (for example, where you have the designation ".", such as xxx.where (... etc.)

SELECT PN.Name, Sum(U.Amount)
FROM Usages as U, ProcessNames as PN
WHERE PN.Id == U.ProcessNameId 
   AND U.Datetime BETWEEN '2010-01-08' AND '2010-10-11'
Group By PN.Name
+3
source share
2 answers

Method-based query:
To implement this in lambda, we need to use 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:
And the same query in the syntax of the query expression:

var query = 
    from 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 
    into g                      
    select new { Name = pn.Name, 
                 Sum = g.Sum(u => u.Amount) };


SQL:
, Sql , :

string sqlCommand = ((ObjectQuery)query).ToTraceString();


:
GroupJoin, , :

:
:

+5

( , ):

from u in context.Usages
join pn in context.ProcessNames on pn.Id equals u.ProcessNameId
where u.Datetime >= new DateTime(2010, 1, 8) && u.Datetime <= new DateTime(2010, 10, 11)
group pn by pn.Name into g
select new { Name = pn.Name , sum = g.Sum(u => u.Amount) };

. ( , ), LinqPad . LinqPad, , - (.. "." ), .

+1

Source: https://habr.com/ru/post/1768542/