forked from FortuneN/Kay.DataAccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResultSet.cs
188 lines (161 loc) · 2.59 KB
/
ResultSet.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
using System.Collections.Generic;
using System.Linq;
namespace Kay.DataAccess
{
public class ResultSet<T>
{
//Fields
private List<T> records;
private int pageSize;
private int pageIndex;
private long recordCount;
//Properties
public List<T> Records
{
get
{
return records;
}
}
public long PageSize
{
get
{
return pageSize;
}
}
public long PageIndex
{
get
{
return pageIndex;
}
}
public long RecordCount
{
get
{
return recordCount;
}
}
public long PageCount
{
get
{
var pageCount = (recordCount / pageSize);
if ((recordCount % pageSize) > 0) pageCount += 1;
return pageCount;
}
}
public long FirstPageIndex
{
get
{
return 0;
}
}
public long LastPageIndex
{
get
{
return PageCount - 1;
}
}
public long PreviousPageIndex
{
get
{
long previousPageIndex = pageIndex - 1;
if (previousPageIndex < FirstPageIndex) previousPageIndex = FirstPageIndex;
return previousPageIndex;
}
}
public long NextPageIndex
{
get
{
long nextPageIndex = pageIndex + 1;
if (nextPageIndex > LastPageIndex) nextPageIndex = LastPageIndex;
return nextPageIndex;
}
}
public long PageNumber
{
get
{
return pageIndex + 1;
}
}
public long FirstPageNumber
{
get
{
return 1;
}
}
public long LastPageNumber
{
get
{
return LastPageIndex + 1;
}
}
public long NextPageNumber
{
get
{
return NextPageIndex + 1;
}
}
public long PreviousPageNumber
{
get
{
return PreviousPageIndex + 1;
}
}
public bool HasRecords
{
get
{
return recordCount != 0;
}
}
public bool HasNextPage
{
get
{
return PageNumber < LastPageNumber;
}
}
public bool HasPreviousPage
{
get
{
return PageNumber > FirstPageNumber;
}
}
public bool HasMoreThanOnePage
{
get
{
return PageCount > 1;
}
}
//Constructors
public ResultSet(IQueryable<T> query, int? pageSize = null, int? pageIndex = null)
{
this.pageSize = (!pageSize.HasValue || pageSize <= 0) ? int.MaxValue : pageSize.Value; // default to <all>
this.pageIndex = (!pageIndex.HasValue || pageIndex <= 0) ? 0 : pageIndex.Value; // default to <firstIndex>
this.records = query.Skip(this.pageSize * this.pageIndex).Take(this.pageSize).ToList();
this.recordCount = query.LongCount();
}
public ResultSet(List<T> list)
{
this.records = list;
this.pageIndex = 0;
this.pageSize = list.Count;
this.recordCount = list.Count;
}
}
}