I would suggest ordering the PersonId query by switching to LINQ on objects via AsEnumerable() (thus, executing it, but without materializing the entire result set in memory, like calling ToList() ), and then use GroupAdjacent from the MoreLINQ package:
This method is implemented using deferred execution and grouping threads. Grouping elements, however, are buffered. Therefore, each grouping will be received as soon as it is completed, and until the next grouping.
var query = from p in personList join a in accountList on p.Id equals a.PersonId where a.Amount < 100 orderby a.PersonId select a; var groups = query.AsEnumerable() .GroupAdjacent(a => a.PersonId) .Where(g => g.Sum(a => a.Amount) > 1000);
The AsEnumerable() three works great with the EF query provider. Regardless of whether it works with the LINQ to CRM provider, it really depends on how the provider implements the GetEnumerator() method - if it still tries to unload the entire query result, then you're out of luck.
source share