C#のページングクラスを共有
11309 ワード
くだらないことは言わないでコードだけ:
1 using System.Linq;
2 using System.Collections.Generic;
3
4 namespace CommonLibrary
5 {
6 public class PagedList<T> : List<T>
7 {
8 #region Properties
9
10 public int PageIndex { get; private set; }
11
12 public int PageSize { get; private set; }
13
14 public int TotalCount { get; private set; }
15
16 public int TotalPages { get; private set; }
17
18 public bool HasPreviousPage
19 {
20 get { return (PageIndex > 0); }
21 }
22 public bool HasNextPage
23 {
24 get { return (PageIndex + 1 < TotalPages); }
25 }
26
27 #endregion
28 //http://www.cnblogs.com/roucheng/
29 #region Constructors
30
31 public PagedList(IQueryable<T> source, int pageIndex, int pageSize)
32 {
33 if (source == null || source.Count() < 1)
34 throw new System.ArgumentNullException("source");
35
36 int total = source.Count();
37 this.TotalCount = total;
38 this.TotalPages = total / pageSize;
39
40 if (total % pageSize > 0)
41 TotalPages++;
42
43 this.PageSize = pageSize;
44 this.PageIndex = pageIndex;
45 this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
46 }
47
48 public PagedList(IList<T> source, int pageIndex, int pageSize)
49 {
50 if (source == null || source.Count() < 1)
51 throw new System.ArgumentNullException("source");
52
53 TotalCount = source.Count();
54 TotalPages = TotalCount / pageSize;
55
56 if (TotalCount % pageSize > 0)
57 TotalPages++;
58
59 this.PageSize = pageSize;
60 this.PageIndex = pageIndex;
61 this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize).ToList());
62 }
63
64 public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
65 {
66 if (source == null || source.Count() < 1)
67 throw new System.ArgumentNullException("source");
68
69 TotalCount = totalCount;
70 TotalPages = TotalCount / pageSize;
71
72 if (TotalCount % pageSize > 0)
73 TotalPages++;
74
75 this.PageSize = pageSize;
76 this.PageIndex = pageIndex;
77 this.AddRange(source);
78 }
79
80 #endregion
81 }
82 }