Loading

c# 分页 PaginatedList<TResult>

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 
 5 namespace Microestc.PaginatedList
 6 {
 7     public class PaginatedList<TResult> : List<TResult>
 8     {
 9         public PaginatedList() : base() { }
10 
11         public PaginatedList(IEnumerable<TResult> items, int totalCount, int pageIndex, int pageSize) : base(items)
12         {
13             TotalCount = totalCount;
14             PageCount = (totalCount + pageSize - 1) / pageSize;
15             PageIndex = pageIndex;
16             PageSize = pageSize;
17             HasPrev = pageIndex > 1;
18             HasNext = pageIndex < PageCount;
19         }
20 
21         public virtual int TotalCount { get; private set; }
22 
23         public virtual int PageCount { get; private set; }
24 
25         public virtual int PageIndex { get; private set; }
26 
27         public virtual int PageSize { get; private set; }
28 
29         public virtual bool HasPrev { get; private set; }
30 
31         public virtual bool HasNext { get; private set; }
32     }
33 
34     public static class PaginatedListExtensions
35     {
36         public static PaginatedList<TResult> PaginatedList<TResult>(this IEnumerable<TResult> source, int pageIndex, int pageSize)
37         {
38             var count = source.Count();
39             var items = source.Skip((pageIndex - 1) * pageSize).Take(pageSize);
40             return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
41         }
42 
43         public static PaginatedList<TResult> PaginatedList<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector, int pageIndex, int pageSize)
44         {
45             var count = source.Count();
46             var items = source.Skip((pageIndex - 1) * pageSize).Take(pageSize).Select(selector);
47             return new PaginatedList<TResult>(items, count, pageIndex, pageSize);
48         }
49     }
50 }
posted @ 2019-12-28 23:34  microestc  阅读(532)  评论(0编辑  收藏  举报