How do I use Linq for paging a generic collection?

后端 未结 2 1905
太阳男子
太阳男子 2021-02-19 06:20

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 MyCus

2条回答
  •  北海茫月
    2021-02-19 06:40

    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 solid code blog.

提交回复
热议问题