.netsql-serverlinqpagination

Linq how to select only a small subset of a large result set?


I have a selection of a huge set of data and I would like to use a paging and to select only k elements, starting from the o'th element in the set, to support my paging. I don't like the idea of loading the whole data set with linq and then get only a subset, using the GetRange method, because the amount of elements can be very huge. For instance, if I have 6 000 000 rows in a table and want to show them, using a paging size of 10 and we are looking at the 5'th page, I would like to generate an SQL Server query, using Linq, which will select only 10 elements of my large table of 6 000 000 rows, because this way my application will be more efficient.

Does anybody know about a support for paging in Linq?

Thank you in advance,

Lajos Árpád.


Solution

  • Use Skip and Take.

    collection.Skip(pageIndex * pageSize).Take(pageSize);