How to execute a group in LINQ and get an Iqueryable or custom class object?

Here is my request -

var data = Goaldata.GroupBy(c => c.GoalId).ToList(); 

This returns a grouping object, and I want an Iqueryable object that I can directly query to get the data, while in this case I need to loop through foreach () and then get the data. Is there any other way to group by LINQ, which returns directly as an Iqueryable or List, similar to what happens for order in LINQ.

+4
source share
3 answers

The easiest way is probably

 var data = Goaldata.GroupBy(c => c.GoalId).SelectMany(c => c).ToList(); 

In the sense of OO, they are not really grouped, but they are ordered together with the groups.

+8
source

While the accepted answer is correct, it seems unnecessarily complex. Assuming GoalId is int , you can simply use OrderBy :

 var data = Goaldata.OrderBy(c => c.GoalId).ToList(); 
+4
source

Or .GroupBy(c => c.GoalId).AsQueryable()...

0
source

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


All Articles