LINQ expression precedence with Skip(), Take() and OrderBy()

Posted by Robert Koritnik on Stack Overflow See other posts from Stack Overflow or by Robert Koritnik
Published on 2010-03-22T16:22:51Z Indexed on 2010/03/22 16:31 UTC
Read the original article Hit count: 318

I'm using LINQ to Entities and display paged results. But I'm having issues with the combination of Skip(), Take() and OrderBy() calls.

Everything works fine, except that OrderBy() is assigned too late. It's executed after result set has been cut down by Skip() and Take().

So each page of results has items in order. But ordering is done on a page handful of data instead of ordering of the whole set and then limiting those records with Skip() and Take().

How do I set precedence with these statements?

My example (simplified)

var query = ctx.EntitySet.Where(/* filter */).OrderBy(/* expression */);
int total = query.Count();
var result = query.Skip(n).Take(x).ToList();

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about linq-to-entities