forked from grandnode/grandnode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPagedList.cs
120 lines (107 loc) · 4.07 KB
/
PagedList.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using MongoDB.Driver;
using MongoDB.Driver.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Grand.Core
{
/// <summary>
/// Paged list
/// </summary>
/// <typeparam name="T">T</typeparam>
[Serializable]
public class PagedList<T> : List<T>, IPagedList<T>
{
private void Init(IMongoQueryable<T> source, int pageIndex, int pageSize, int? totalCount = null)
{
if (source == null)
throw new ArgumentNullException("source");
if (pageSize <= 0)
throw new ArgumentException("pageSize must be greater than zero");
var taskCount = source.CountAsync();
source = totalCount == null ? source.Skip(pageIndex * pageSize).Take(pageSize) : source;
AddRange(source);
taskCount.Wait();
TotalCount = totalCount ?? (int)taskCount.Result;
if (pageSize > 0)
{
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
}
PageSize = pageSize;
PageIndex = pageIndex;
}
private void Init(IEnumerable<T> source, int pageIndex, int pageSize, int? totalCount = null)
{
if (source == null)
throw new ArgumentNullException("source");
if (pageSize <= 0)
throw new ArgumentException("pageSize must be greater than zero");
TotalCount = totalCount ?? source.Count();
if (pageSize > 0)
{
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
}
PageSize = pageSize;
PageIndex = pageIndex;
source = totalCount == null ? source.Skip(pageIndex * pageSize).Take(pageSize) : source;
AddRange(source);
}
public PagedList()
{
}
public PagedList(IEnumerable<T> source, int pageIndex, int pageSize)
{
Init(source, pageIndex, pageSize);
}
public PagedList(IMongoQueryable<T> source, int pageIndex, int pageSize)
{
Init(source, pageIndex, pageSize);
}
public PagedList(IAggregateFluent<T> source, int pageIndex, int pageSize)
{
var range = source.Skip(pageIndex * pageSize).Limit(pageSize+1).ToList();
int total = range.Count > pageSize ? range.Count : pageSize;
this.TotalCount = source.ToListAsync().Result.Count;
if(pageSize > 0)
this.TotalPages = total / pageSize;
if (total % pageSize > 0)
TotalPages++;
this.PageSize = pageSize;
this.PageIndex = pageIndex;
this.AddRange(range.Take(pageSize));
}
public PagedList(IMongoCollection<T> source, FilterDefinition<T> filterdefinition, SortDefinition<T> sortdefinition, int pageIndex, int pageSize)
{
TotalCount = (int)source.CountDocuments(filterdefinition);
AddRange(source.Find(filterdefinition).Sort(sortdefinition).Skip(pageIndex * pageSize).Limit(pageSize).ToListAsync().Result);
if (pageSize > 0)
{
TotalPages = TotalCount / pageSize;
if (TotalCount % pageSize > 0)
TotalPages++;
}
this.PageSize = pageSize;
this.PageIndex = pageIndex;
}
public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
{
Init(source, pageIndex, pageSize, totalCount);
}
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int TotalCount { get; private set; }
public int TotalPages { get; private set; }
public bool HasPreviousPage
{
get { return (PageIndex > 0); }
}
public bool HasNextPage
{
get { return (PageIndex + 1 < TotalPages); }
}
}
}