I have been trying out different ways to write queries that include "group by" and "count()" using Entity Framework Core and LINQ. But the SQL that is generated simply returns all records and then the "group by" and "count()" is done in memory.
Currently I am trying to write a query that is equivalent to the following SQL:
select
e.EngagementId,
e.Name,
c.ClientName,
count(distinct ed.EngagementDocumentId) as DocumentCount,
count(distinct es.EngagementSurveyId) as SurveyCount
from Engagement e
inner join Client c on c.ClientId = e.ClientId
left outer join EngagementDocument ed on ed.EngagementId = e.EngagementId
left outer join EngagementSurvey es on es.EngagementId = e.EngagementId
group by e.EngagementId, e.Name, c.ClientName
My question is: How can I write a query that generates similar SQL?
GroupByyou addSelectwith only key / aggregates, the query should be translated to SQL. - Ivan Stoev