I want to return a list of a certain entity grouped by a certain property, ordered descending by timestamp and paginated (using Skip and Take). What I got is this:
container.CoinMessageSet.Where(
c => c.MessageState != MessageStateType.Closed &&
(c.DonorOperator.OperatorCode.Equals("opcode") ||
c.RecipientOperator.OperatorCode.Equals("opcode"))
).OrderByDescending(c => c.TimeStamp)
.GroupBy(c => c.Reference).Skip(x).Take(100);
Upon execution I got the Exception:
The method 'Skip' is only supported for sorted input in LINQ to Entities.
The method 'OrderBy' must be called before the method 'Skip'.
…I called OrderBy() (albeit Descending) and I called it before Skip()! What am I missing?
You haven’t ordered the groups; you need to do that before you can page. For example:
(you can also substitute
OrderByDescendingif you want the groups in reverse order)Also: since you are grouping, the order in the original data is largely meaningless; you could probably remove the
OrderByDescending(c => c.TimeStamp).So net result:
or possibly: