forked from smartstore/SmartStoreNET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagedList`T.cs
More file actions
332 lines (279 loc) · 5.75 KB
/
Copy pathPagedList`T.cs
File metadata and controls
332 lines (279 loc) · 5.75 KB
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Collections;
using System.Collections.ObjectModel;
namespace SmartStore.Core
{
public class PagedList<T> : IPagedList<T>, IReadOnlyList<T>, IReadOnlyCollection<T>
{
private IQueryable<T> _query;
private bool _queryIsPagedAlready;
private int _pageIndex;
private int _pageSize;
private int? _totalCount;
private List<T> _list;
/// <param name="pageIndex">The 0-based page index</param>
public PagedList(IEnumerable<T> source, int pageIndex, int pageSize)
{
Guard.NotNull(source, "source");
Init(source.AsQueryable(), pageIndex, pageSize, null);
}
/// <param name="pageIndex">The 0-based page index</param>
public PagedList(IEnumerable<T> source, int pageIndex, int pageSize, int totalCount)
{
Guard.NotNull(source, "source");
Init(source.AsQueryable(), pageIndex, pageSize, totalCount);
}
private void Init(IQueryable<T> source, int pageIndex, int pageSize, int? totalCount)
{
Guard.NotNull(source, nameof(source));
Guard.PagingArgsValid(pageIndex, pageSize, "pageIndex", "pageSize");
_query = source;
_queryIsPagedAlready = totalCount.HasValue;
_pageIndex = pageIndex;
_pageSize = pageSize;
_totalCount = totalCount;
}
private void EnsureIsLoaded()
{
if (_list == null)
{
if (_totalCount == null)
{
_totalCount = _query.Count();
}
if (_queryIsPagedAlready)
{
_list = _query.ToList();
}
else
{
_list = ApplyPaging(_query).ToList();
}
}
}
#region IPageable Members
public IQueryable<T> SourceQuery
{
get { return _query; }
}
public IQueryable<T> ApplyPaging(IQueryable<T> query)
{
if (_pageIndex == 0 && _pageSize == int.MaxValue)
{
// Paging unnecessary
return query;
}
else
{
var skip = _pageIndex * _pageSize;
if (query.Provider is IDbAsyncQueryProvider)
{
return query.Skip(() => skip).Take(() => _pageSize);
}
else
{
return query.Skip(skip).Take(_pageSize);
}
}
}
public IPagedList<T> Load(bool force = false)
{
if (force && _list != null)
{
_list.Clear();
_list = null;
}
EnsureIsLoaded();
return this;
}
public int PageIndex
{
get { return _pageIndex; }
set { _pageIndex = value; }
}
public int PageSize
{
get { return _pageSize; }
set { _pageSize = value; }
}
public int TotalCount
{
get
{
if (!_totalCount.HasValue)
{
_totalCount = _query.Count();
}
return _totalCount.Value;
}
set
{
_totalCount = value;
}
}
public int PageNumber
{
get
{
return this.PageIndex + 1;
}
set
{
this.PageIndex = value - 1;
}
}
public int TotalPages
{
get
{
var total = this.TotalCount / this.PageSize;
if (this.TotalCount % this.PageSize > 0)
total++;
return total;
}
}
public bool HasPreviousPage
{
get
{
return this.PageIndex > 0;
}
}
public bool HasNextPage
{
get
{
return (this.PageIndex < (this.TotalPages - 1));
}
}
public int FirstItemIndex
{
get
{
return (this.PageIndex * this.PageSize) + 1;
}
}
public int LastItemIndex
{
get
{
return Math.Min(this.TotalCount, ((this.PageIndex * this.PageSize) + this.PageSize));
}
}
public bool IsFirstPage
{
get
{
return (this.PageIndex <= 0);
}
}
public bool IsLastPage
{
get
{
return (this.PageIndex >= (this.TotalPages - 1));
}
}
#endregion
#region IList<T> Members
public void Add(T item)
{
EnsureIsLoaded();
_list.Add(item);
}
public void Clear()
{
if (_list != null)
{
_list.Clear();
_list = null;
}
}
public bool Contains(T item)
{
EnsureIsLoaded();
return _list.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
EnsureIsLoaded();
_list.CopyTo(array, arrayIndex);
}
public bool Remove(T item)
{
if (_list != null)
{
return _list.Remove(item);
}
return false;
}
public int Count
{
get
{
EnsureIsLoaded();
return _list.Count;
}
}
public bool IsReadOnly
{
get { return false; }
}
public int IndexOf(T item)
{
EnsureIsLoaded();
return _list.IndexOf(item);
}
public void Insert(int index, T item)
{
EnsureIsLoaded();
_list.Insert(index, item);
}
public void RemoveAt(int index)
{
if (_list != null)
{
_list.RemoveAt(index);
}
}
public T this[int index]
{
get
{
EnsureIsLoaded();
return _list[index];
}
set
{
EnsureIsLoaded();
_list[index] = value;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
EnsureIsLoaded();
return _list.GetEnumerator();
}
#endregion
#region Utils
public void AddRange(IEnumerable<T> collection)
{
EnsureIsLoaded();
_list.AddRange(collection);
}
public ReadOnlyCollection<T> AsReadOnly()
{
EnsureIsLoaded();
return _list.AsReadOnly();
}
#endregion
}
}