How do I use Linq for paging a generic collection?

Question :

How do I use Linq for paging a generic collection?,

Answer :

I’ve got a System.Generic.Collections.List(Of MyCustomClass) type object.

Given integer varaibles pagesize and pagenumber, how can I query only any single page of MyCustomClass objects?

,

If you have your linq-query that contains all the rows you want to display, this code can be used:

var pageNum = 3;  var pageSize = 20;  query = query.Skip((pageNum - 1) * pageSize).Take(pageSize);  

You can also make an extension method on the object to be able to write

query.Page(2,50)  

to get the first 50 records of page 2. If that is want you want, the information is on the

Read More  C# 3.0 auto-properties — useful or not?

That’s the answer How do I use Linq for paging a generic collection?, Hope this helps those looking for an answer. Then we suggest to do a search for the next question and find the answer only on our site.

Disclaimer :

The answers provided above are only to be used to guide the learning process. The questions above are open-ended questions, meaning that many answers are not fixed as above. I hope this article can be useful, Thank you