-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagination.cs
More file actions
359 lines (340 loc) · 13.5 KB
/
Pagination.cs
File metadata and controls
359 lines (340 loc) · 13.5 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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
using System;
using System.Linq;
using System.Data.Entity;
using System.Collections.Generic;
namespace QueryablePagination
{
/// <summary>
/// <para>
/// General set of properties needed for pagination of queries without looking into the queried data.
/// </para>
/// <para>
/// This interface is intended to be used as a model a "generic" Partial View for pagination on MVC,
/// allowing better code reuse in a project.
/// </para>
/// </summary>
public interface IPaged
{
/// <summary>
/// Current page on the results.
/// </summary>
int Page
{
get;
}
/// <summary>
/// Number of result pages.
/// </summary>
int PageCount
{
get;
}
/// <summary>
/// Number of records to read on each page.
/// </summary>
int PerPage
{
get;
}
/// <summary>
/// Number of records avaliable to read.
/// </summary>
int Total
{
get;
}
}
/// <summary>
/// Standard implementation of <see cref="QueryablePagination.IPaged"/>,
/// providing basic pagination functionality without loading the data from the query,
/// only the number of avaliable records is read.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied</typeparam>
/// <typeparam name="TResult">Type in wich the query results will be stored</typeparam>
public abstract class Pagination<TQueryable, TResult> : IPaged
{
/// <summary>
/// Data that will be loaded by the query, already on the desired format.
/// </summary>
protected TResult Data
{
get;
set;
}
/// <inheritdoc/>
public int Page
{
get;
}
/// <inheritdoc />
public int PageCount
{
get;
}
/// <inheritdoc />
public int PerPage
{
get;
}
/// <summary>
/// Readonly access to the loaded query data.
/// </summary>
public TResult Results
{
get
{
return Data;
}
}
/// <inheritdoc />
public int Total
{
get;
}
/// <summary>
/// <para>
/// Number of records to skip from the begining.
/// </para>
/// <para>
/// Meant to be used only internally, for pagination.
/// </para>
/// </summary>
protected int ToSkip;
/// <summary>
/// Basic constructor that reads the number of avaliable records from
/// the query and does some pagination calcs.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public Pagination(
IOrderedQueryable<TQueryable> Query,
int Page = 1,
int PerPage = 100
)
{
// Getting the number of registers
Total = Query.Count();
this.Page = Page;
this.PerPage = PerPage;
PageCount = (int)Math.Ceiling((decimal)(Total / PerPage));
ToSkip = (Page - 1) * PerPage;
}
}
/// <summary>
/// Array-based implementation of <see cref="QueryablePagination.Pagination"/>,
/// implementing pagination with an array of <typeparamref name="TQueryable"/>.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied.</typeparam>
public class PagedArray<TQueryable> : Pagination<TQueryable, Array>
{
/// <summary>
/// Creates a new <see cref="QueryablePagination.Pagination"/> instance with
/// data stored in an Array.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public PagedArray(
IOrderedQueryable<TQueryable> Query,
int Page = 1,
int PerPage = 100
) : base(Query, Page, PerPage)
{
Data = Query.Skip(ToSkip).Take(PerPage).ToArray();
}
}
/// <summary>
/// Dictionary-based implementation of <see cref="QueryablePagination.Pagination"/>,
/// implementing pagination with a dictionary of <typeparamref name="TQueryable"/>
/// that uses <typeparamref name="TKey"/> keys.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied.</typeparam>
/// <typeparam name="TKey">Type of key for the dictionary.</typeparam>
public class PagedDictionary<TKey, TQueryable> : Pagination<TQueryable, Dictionary<TKey, TQueryable>>
{
/// <summary>
/// Creates a new <see cref="QueryablePagination.Pagination"/> instance with
/// data stored in a Dictionary.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="keySelector">Function to get keys for the records.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public PagedDictionary(
IOrderedQueryable<TQueryable> Query,
Func<TQueryable, TKey> keySelector,
int Page = 1,
int PerPage = 100
) : base(Query, Page, PerPage)
{
Data = Query.Skip(ToSkip).Take(PerPage).ToDictionary(keySelector);
}
}
/// <summary>
/// HashSet-based implementation of <see cref="QueryablePagination.Pagination"/>,
/// implementing pagination with a HashSet of <typeparamref name="TQueryable"/>.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied.</typeparam>
public class PagedHashSet<TQueryable> : Pagination<TQueryable, HashSet<TQueryable>>
{
/// <summary>
/// Creates a new <see cref="QueryablePagination.Pagination"/> instance with
/// data stored in a HashSet.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public PagedHashSet(
IOrderedQueryable<TQueryable> Query,
int Page = 1,
int PerPage = 100
) : base(Query, Page, PerPage)
{
Data = Query.Skip(ToSkip).Take(PerPage).ToHashSet();
}
}
/// <summary>
/// Lookup-based implementation of <see cref="QueryablePagination.Pagination"/>,
/// implementing pagination with a lookup of <typeparamref name="TQueryable"/>
/// that uses <typeparamref name="TKey"/> keys.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied.</typeparam>
/// <typeparam name="TKey">Type of key for the lookup.</typeparam>
public class PagedLookup<TKey, TQueryable> : Pagination<TQueryable, ILookup<TKey, TQueryable>>
{
/// <summary>
/// Creates a new <see cref="QueryablePagination.Pagination"/> instance with
/// data stored in a Lookup.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="keySelector">Function to get keys for the records.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public PagedLookup(
IOrderedQueryable<TQueryable> Query,
Func<TQueryable, TKey> keySelector,
int Page = 1,
int PerPage = 100
) : base(Query, Page, PerPage)
{
Data = Query.Skip(ToSkip).Take(PerPage).ToLookup(keySelector);
}
}
/// <summary>
/// List-based implementation of <see cref="QueryablePagination.Pagination"/>,
/// implementing pagination with a list of <typeparamref name="TQueryable"/>.
/// </summary>
/// <typeparam name="TQueryable">Type of Entity that will be queryied.</typeparam>
public class PagedList<TQueryable> : Pagination<TQueryable, List<TQueryable>>
{
/// <summary>
/// Creates a new <see cref="QueryablePagination.Pagination"/> instance with
/// data stored in a List.
/// </summary>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public PagedList(
IOrderedQueryable<TQueryable> Query,
int Page = 1,
int PerPage = 100
) : base(Query, Page, PerPage)
{
Data = Query.Skip(ToSkip).Take(PerPage).ToList();
}
}
/// <summary>
/// Set of extension methods that allow to easily create a <see cref="QueryablePagination.Pagination"/>
/// instance from a <see cref="System.Linq.IOrderedQueryable"/>.
/// </summary>
public static class PaginationExtensions
{
/// <summary>
/// Creates an instance of <see cref="QueryablePagination.Pagination"/>,
/// that uses an array of <typeparamref name="T"/> to store results.
/// </summary>
/// <typeparam name="T">Type of Entity that will be queryied.</typeparam>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public static PagedArray<T> ToPagedArray<T>(
this IOrderedQueryable<T> Query,
int Page = 1,
int PerPage = 100
)
{
return new PagedArray<T>(Query, Page, PerPage);
}
/// <summary>
/// Creates an instance of <see cref="QueryablePagination.Pagination"/>,
/// that uses a Dictionary of <typeparamref name="T"/> with
/// <typeparamref name="TKey"/> keys to store results.
/// </summary>
/// <typeparam name="T">Type of Entity that will be queryied.</typeparam>
/// <typeparam name="TKey">Type of key for the dictionary.</typeparam>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public static PagedDictionary<TKey, T> ToPagedDictionary<TKey, T>(
this IOrderedQueryable<T> Query,
Func<T, TKey> keySelector,
int Page = 1,
int PerPage = 100
)
{
return new PagedDictionary<TKey, T>(Query, keySelector, Page, PerPage);
}
/// <summary>
/// Creates an instance of <see cref="QueryablePagination.Pagination"/>,
/// that uses a HashSet of <typeparamref name="T"/> to store results.
/// </summary>
/// <typeparam name="T">Type of Entity that will be queryied.</typeparam>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public static PagedHashSet<T> ToPagedHashSet<T>(
this IOrderedQueryable<T> Query,
int Page = 1,
int PerPage = 100
)
{
return new PagedHashSet<T>(Query, Page, PerPage);
}
/// <summary>
/// Creates an instance of <see cref="QueryablePagination.Pagination"/>,
/// that uses a Lookup of <typeparamref name="T"/> with
/// <typeparamref name="TKey"/> keys to store results.
/// </summary>
/// <typeparam name="T">Type of Entity that will be queryied.</typeparam>
/// <typeparam name="TKey">Type of key for the lookup.</typeparam>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public static PagedLookup<TKey, T> ToPagedLookup<TKey, T>(
this IOrderedQueryable<T> Query,
Func<T, TKey> keySelector,
int Page = 1,
int PerPage = 100
)
{
return new PagedLookup<TKey, T>(Query, keySelector, Page, PerPage);
}
/// <summary>
/// Creates an instance of <see cref="QueryablePagination.Pagination"/>,
/// that uses a list of <typeparamref name="T"/> to store results.
/// </summary>
/// <typeparam name="T">Type of Entity that will be queryied.</typeparam>
/// <param name="Query">The query to paginate, must be ordered.</param>
/// <param name="Page">Current page of the results.</param>
/// <param name="PerPage">Number of records to read on each page.</param>
public static PagedList<T> ToPagedList<T>(
this IOrderedQueryable<T> Query,
int Page = 1,
int PerPage = 100
)
{
return new PagedList<T>(Query, Page, PerPage);
}
}
}