Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 760
Mike Perrenoud Avatar asked Mar 19 '26 21:03

Mike Perrenoud


2 Answers

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);
like image 151
akatakritos Avatar answered Mar 22 '26 10:03

akatakritos


You should probably use Skip

return results.Skip(pageSize * pageIndex).Take(pageSize);
like image 39
Mihai Avatar answered Mar 22 '26 10:03

Mihai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!