I have a Action model with Session Navigation Property,
Consider this code:
var x=db.Actions.OrderBy(p => p.Session.Number).ThenBy(p => p.Date);//it's OK
x is a ordered Action, but when grouped on x, group not iterate on x(base on Action.Session) manually on ordered enumerable:
var y=x.GroupBy(p=>p.Session).ToArray()
y have a group(Key,IGrouping) of sessions but why group.Key not ordered base on Session.Number?
How to i reached a group of Session order by number and each group ordered by date?
Because it’s
Enumerable.GroupBythat preserves order. No such promise is made forQueryable.GroupBy.From the documentation of the former:
You’re calling the latter, and the above is not mentioned. Call
OrderByafterGroupByto make it work.Update: since you apparently want to sort on more than just the GroupBy key, you should be able to use another GroupBy overload to specify that each session’s list of actions is to be sorted: