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?
LINQ includes five sorting operators: OrderBy, OrderByDescending, ThenBy, ThenByDescending and Reverse. LINQ query syntax does not support OrderByDescending, ThenBy, ThenByDescending and Reverse. It only supports 'Order By' clause with 'ascending' and 'descending' sorting direction.
The OrderBy() Method, first sort the elements of the sequence or collection in ascending order after that ThenBy() method is used to again sort the result of OrderBy() method in ascending order.
In a LINQ query, you are always working with objects. You use the same basic coding patterns to query and transform data in XML documents, SQL databases, ADO.NET Datasets, . NET collections, and any other format for which a LINQ provider is available.
First(); var next = items . OrderBy(item => item.Id) . First(item => item.Id > currentId); var next = items .
You haven't ordered the groups; you need to do that before you can page. For example:
.GroupBy(c => c.Reference).OrderBy(grp => grp.Key).Skip(x).Take(100);
(you can also substitute OrderByDescending
if 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:
var query = container.CoinMessageSet.Where(
c => c.MessageState != MessageStateType.Closed &&
(c.DonorOperator.OperatorCode.Equals("opcode") ||
c.RecipientOperator.OperatorCode.Equals("opcode"))
).GroupBy(c => c.Reference).OrderBy(grp => grp.Key)
.Skip(x).Take(100);
or possibly:
var query = (from c in container.CoinMessageSet
where c.MessageState != MessageStateType.Closed &&
(c.DonorOperator.OperatorCode == "opcode" ||
c.RecipientOperator.OperatorCode == "opcode")
group c by c.Reference into grp
orderby grp.Key
select grp).Skip(x).Take(100);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With