forked from SharpRepository/SharpRepository
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagingOptions.cs
More file actions
114 lines (100 loc) · 3.69 KB
/
PagingOptions.cs
File metadata and controls
114 lines (100 loc) · 3.69 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
using System;
using System.Linq;
using System.Linq.Expressions;
namespace SharpRepository.Repository.Queries
{
/// <summary>
/// Used to define the paging criteria on queries run against a repository.
/// </summary>
/// <typeparam name="T">The entity type of the repository.</typeparam>
/// <typeparam name="TSortKey">The type of the property that is being sorted.</typeparam>
public class PagingOptions<T, TSortKey> : SortingOptions<T, TSortKey>, IPagingOptions
{
public int PageSize { get; set; }
public int PageNumber { get; set; }
public int Skip { get { return (PageNumber - 1) * PageSize; } }
public int Take { get { return PageSize; } }
public int TotalItems { get; set; }
public PagingOptions(int pageNumber, int pageSize, Expression<Func<T, TSortKey>> sortExpression, bool isDescending = false)
: base(sortExpression, isDescending)
{
PageSize = pageSize;
PageNumber = pageNumber;
}
/// <summary>
/// Applies paging to the specified query.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Paged results.</returns>
public override IQueryable<T> Apply(IQueryable<T> query)
{
query = base.Apply(query);
TotalItems = query.Count();
if (Skip > 0 || Take > 0)
{
return query.Skip(Skip).Take(Take);
}
return query;
}
/// <summary>
/// Used in compiling a unique key for a query
/// </summary>
/// <returns>Unique key for a query</returns>
public override string ToString()
{
return String.Format("PagingOptions<{0},{1}>\nPageSize: {2}\nPageNumber: {3}\nSort: {4}",
(typeof(T)).Name,
(typeof(TSortKey)).Name,
PageSize,
PageNumber,
base.ToString()
);
}
}
/// <summary>
/// Used to define the paging criteria on queries run against a repository.
/// </summary>
/// <typeparam name="T">The entity type of the repository.</typeparam>
public class PagingOptions<T> : SortingOptions<T>, IPagingOptions
{
public int PageSize { get; set; }
public int PageNumber { get; set; }
public int Skip { get { return (PageNumber - 1) * PageSize; } }
public int Take { get { return PageSize; } }
public int TotalItems { get; set; }
public PagingOptions(int pageNumber, int pageSize, string sortProperty, bool isDescending = false)
: base(sortProperty, isDescending)
{
PageSize = pageSize;
PageNumber = pageNumber;
}
/// <summary>
/// Applies paging to the specified query.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Paged results.</returns>
public override IQueryable<T> Apply(IQueryable<T> query)
{
query = base.Apply(query);
TotalItems = query.Count();
if (Skip > 0 || Take > 0)
{
return query.Skip(Skip).Take(Take);
}
return query;
}
/// <summary>
/// Used in compiling a unique key for a query
/// </summary>
/// <returns>Unique key for a query</returns>
public override string ToString()
{
return String.Format("PagingOptions<{0}>\nPageSize: {1}\nPageNumber: {2}\nSort: {3}",
(typeof(T)).Name,
PageSize,
PageNumber,
base.ToString()
);
}
}
}