Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 7 group by

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();
like image 848
RickJames Avatar asked Jun 05 '15 13:06

RickJames


People also ask

What is group by in Entity Framework?

The GROUP BY statement is usually used (mostly with aggregate functions) to group the result of your query by one or more columns.

What's new in EF Core 7?

Highly requested featuresJSON columns: Save and query into JSON-based documents stored in relational database columns. Bulk updates: Efficient, predicate-based updates for many database rows without loading data into memory. Lifecycle hooks: Allow applications to react when interesting things happen in EF code.

Does EF Core support stored procedures?

EF Core already supports querying data from stored procedures. This feature will allow mapping the inserts, updates, and deletes generated by SaveChanges to stored procedures in the database.

Why EF Core?

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version of the popular Entity Framework data access technology. EF Core can serve as an object-relational mapper (O/RM), which: Enables . NET developers to work with a database using .


1 Answers

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.

like image 135
Matt DeKrey Avatar answered Oct 11 '22 13:10

Matt DeKrey