Question

I'm trying to run the following code but when I check the SQL profiler it looks like it runs a full select on the table and then does the grouping after it returns all results from the database. Any help is appreciated.

 var result = _dbContext.LogEvent.GroupBy(x => x.EventLevel)
                                 .Select(g => new 
                                              { 
                                                  eventType = g.Key, 
                                                  total = g.Sum(i => i.Occurrences) 
                                              }) 
                                 .ToList();

Accepted Answer

Updated:

The issue linked below has been closed, and it is slated for EF 2.1.0. You should be able to try it out now using the Preview package!


Original answer:

It doesn't look like this is currently supported, but it looks like someone saw this post and created the linked issue.

The concept is a fairly complex bit of logic, and EF7 is very much in an early phase. .Net's GroupBy doesn't translate directly to SQL's GROUP BY until you follow it up with only aggregates or the Key in a Select. If you're feeling ambitious, you could work on providing a pull request, or continue to use EF6.


Popular Answer

It's probably not going to be supported any time soon - unfortunatelt. Going to cause a lot of frustration because so many people don't read release notes and would just naturally assume it would work as before (EF6 / Linq2SQL).

I realize it can be complex for more complicated things involving whole entities, but it's a shame it isn't even supported for simple queries like yours :-(

https://blogs.msdn.microsoft.com/dotnet/2016/05/16/announcing-entity-framework-core-rc2/



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why