Ok, so I have a table
Table:
Id
Value
If I query my table and group my result by “Value” how can I make it so each of the groups are alphabetized (a group grouped by a “Value”=”a” will come before a group grouped by a “Value” = “z”).
My current query looks something like this:
var Result =
from a in DB.Table
orderby a.Value
group by a.Value into b
select new {Groupz = b};
The other answers are ordering the initial list and then ordering the groups. This works because GroupBy preserves the order of the initial collection. However, if you want to actually order the groups (which was the original question), you want to create the groups first, and then order by the Key of the IGrouping object:
Using query syntax, this looks like this:
Again, we’re doing the group by operation before the order by operation, and actually performing the ordering on the group, not on the original list elements.