Group By Query with Entity Framework

c# entity-framework entity-framework-6 group-by linq

Question

In my application I have Movements associated with a category. I want a list of the most frequent category.

My objects are:

Category: catId, catName
Movement: Movid, movDate, movMount, catId

I think it would have to raise it with a "Group By" query (grouping by catId and getting those more) (Im using Entity Framework 6 in c#)

From already thank you very much!

1
2
4/6/2014 11:35:35 PM

Accepted Answer

i hope this help:

var query = dbContext.Category.Select(u => new 
{ 
    Cat = u, 
    MovementCount = u.Movement.Count() 
})
.ToList()
.OrderByDescending(u => u.MovementCount)
.Select(u => u.Cat)
.ToList();
1
4/7/2014 6:38:48 PM

Popular Answer

IMPORTANT: Entity Framework 7 (now renamed to Entity Framework Core 1.0) does not yet support GroupBy() for translation to GROUP BY in generated SQL. Any grouping logic will run on the client side, which could cause a lot of data to be loaded.

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow