Mike Perrenoud
Mike Perrenoud

Reputation: 67918

Paging with LINQ?

Okay, right now I've got this statement and it's working well (note I've already sorted the list before executing this statement so Reverse is simply allowing me to pop off the last page):

return results.Take(pageSize * pageIndex).Reverse().Take(pageSize);

But there's got to be a more efficient way ... can anybody show me the way?

Thanks!

Upvotes: 2

Views: 941

Answers (2)

Mihai
Mihai

Reputation: 2760

You should probably use Skip

return results.Skip(pageSize * pageIndex).Take(pageSize);

Upvotes: 3

akatakritos
akatakritos

Reputation: 9858

There is a Skip operator.

Bypasses a specified number of elements in a sequence and then returns the remaining elements. http://msdn.microsoft.com/en-us/library/bb358985.aspx

For example:

return results.Skip(pageSize * pageIndex).Take(pageSize);

Upvotes: 6

Related Questions