How do I use Linq for paging a generic collection?

后端 未结 2 1922
太阳男子
太阳男子 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:55

    Hi There is a wicked thing called PagedList which i got when watching a Rob Conery Screen Cast.

    http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

    It has all the Skip and Take stuff built in.

    All you do is call

    var query = from item in DB.Table
    where item.Field == 1
    orderby item.Field2
    select item;
    
    PagedList pagedList = query.ToPagedList(pageIndex, pageSize);
    

    Hope it helps.. I'm using it now and it works ok for linq to entities. With Linq to entities you have to perform an Orderby before you can use Skip and Take.

提交回复
热议问题