Because it is Enumerable.GroupBy , which keeps order. This is not promised for Queryable.GroupBy . From the documentation of the first:
IGrouping objects (Of TKey, TElement) are displayed in an order based on the order of the elements in the source that produced the first key of each IGrouping (Of TKey, TElement). Elements in the grouping are listed in the order they appear in the source.
You invoke the latter, and is not mentioned above. Call GroupBy after GroupBy to make it work.
Refresh . Since you apparently want to sort not only the GroupBy key, you can use another GroupBy overload to indicate that each list of action sessions should be sorted:
db.Actions.GroupBy( p => p.Session, (session, actions) => new { Session = session, Actions = actions.OrderBy(p => p.Date) }).OrderBy(p => p.Session.Number).ToArray();
source share