1
1
using System . Collections ;
2
2
using System . Collections . Generic ;
3
+ using System . Diagnostics ;
4
+ using System . Linq . Dynamic . Core . Extensions ;
3
5
using System . Linq . Dynamic . Core . Validation ;
4
6
using System . Linq . Expressions ;
7
+ using System . Reflection ;
5
8
using JetBrains . Annotations ;
6
9
7
10
namespace System . Linq . Dynamic . Core
@@ -14,6 +17,77 @@ namespace System.Linq.Dynamic.Core
14
17
public static class BasicQueryable
15
18
{
16
19
#region IQueryable Adjustors
20
+ /// <summary>
21
+ /// Returns the elements as paged.
22
+ /// </summary>
23
+ /// <param name="source">The sequence to return elements from.</param>
24
+ /// <param name="page">The page to return.</param>
25
+ /// <param name="pageSize">The number of elements per page.</param>
26
+ /// <returns>A <see cref="IQueryable"/> that contains the paged elements.</returns>
27
+ public static IQueryable Page ( [ NotNull ] this IQueryable source , int page , int pageSize )
28
+ {
29
+ Check . NotNull ( source , nameof ( source ) ) ;
30
+ Check . Condition ( page , p => p > 0 , nameof ( page ) ) ;
31
+ Check . Condition ( pageSize , ps => ps > 0 , nameof ( pageSize ) ) ;
32
+
33
+ return source . Skip ( ( page - 1 ) * pageSize ) . Take ( pageSize ) ;
34
+ }
35
+
36
+ public static IQueryable < TSource > Page < TSource > ( [ NotNull ] this IQueryable < TSource > source , int page , int pageSize )
37
+ {
38
+ Check . NotNull ( source , nameof ( source ) ) ;
39
+ Check . Condition ( page , p => p > 0 , nameof ( page ) ) ;
40
+ Check . Condition ( pageSize , ps => ps > 0 , nameof ( pageSize ) ) ;
41
+
42
+ return Queryable . Take ( Queryable . Skip ( source , ( page - 1 ) * pageSize ) , pageSize ) ;
43
+ }
44
+
45
+ /// <summary>
46
+ /// Returns the elements as paged and include the CurrentPage, PageCount, PageSize and RowCount.
47
+ /// http://weblogs.asp.net/gunnarpeipman/returning-paged-results-from-repositories-using-pagedresult-lt-t-gt
48
+ /// </summary>
49
+ /// <param name="source">The sequence to return elements from.</param>
50
+ /// <param name="page">The page to return.</param>
51
+ /// <param name="pageSize">The number of elements per page.</param>
52
+ /// <returns>PagedResult</returns>
53
+ public static PagedResult PageResult ( [ NotNull ] this IQueryable source , int page , int pageSize )
54
+ {
55
+ Check . NotNull ( source , nameof ( source ) ) ;
56
+ Check . Condition ( page , p => p > 0 , nameof ( page ) ) ;
57
+ Check . Condition ( pageSize , ps => ps > 0 , nameof ( pageSize ) ) ;
58
+
59
+ var result = new PagedResult
60
+ {
61
+ CurrentPage = page ,
62
+ PageSize = pageSize ,
63
+ RowCount = source . Count ( )
64
+ } ;
65
+
66
+ result . PageCount = ( int ) Math . Ceiling ( ( double ) result . RowCount / pageSize ) ;
67
+ result . Queryable = Page ( source , page , pageSize ) ;
68
+
69
+ return result ;
70
+ }
71
+
72
+ public static PagedResult < TSource > PageResult < TSource > ( [ NotNull ] this IQueryable < TSource > source , int page , int pageSize )
73
+ {
74
+ Check . NotNull ( source , nameof ( source ) ) ;
75
+ Check . Condition ( page , p => p > 0 , nameof ( page ) ) ;
76
+ Check . Condition ( pageSize , ps => ps > 0 , nameof ( pageSize ) ) ;
77
+
78
+ var result = new PagedResult < TSource >
79
+ {
80
+ CurrentPage = page ,
81
+ PageSize = pageSize ,
82
+ RowCount = Queryable . Count ( source )
83
+ } ;
84
+
85
+ result . PageCount = ( int ) Math . Ceiling ( ( double ) result . RowCount / pageSize ) ;
86
+ result . Queryable = Page ( source , page , pageSize ) ;
87
+
88
+ return result ;
89
+ }
90
+
17
91
/// <summary>
18
92
/// Returns a specified number of contiguous elements from the start of a sequence.
19
93
/// </summary>
@@ -23,7 +97,7 @@ public static class BasicQueryable
23
97
public static IQueryable Take ( [ NotNull ] this IQueryable source , int count )
24
98
{
25
99
Check . NotNull ( source , nameof ( source ) ) ;
26
- Check . Condition ( count , x => x > 0 , nameof ( source ) ) ;
100
+ Check . Condition ( count , x => x > 0 , nameof ( count ) ) ;
27
101
28
102
return source . Provider . CreateQuery (
29
103
Expression . Call (
@@ -32,6 +106,14 @@ public static IQueryable Take([NotNull] this IQueryable source, int count)
32
106
source . Expression , Expression . Constant ( count ) ) ) ;
33
107
}
34
108
109
+ public static IQueryable < TSource > Take < TSource > ( [ NotNull ] this IQueryable < TSource > source , int count )
110
+ {
111
+ Check . NotNull ( source , nameof ( source ) ) ;
112
+ Check . Condition ( count , x => x > 0 , nameof ( count ) ) ;
113
+
114
+ return Queryable . Take ( source , count ) ;
115
+ }
116
+
35
117
/// <summary>
36
118
/// Bypasses a specified number of elements in a sequence and then returns the remaining elements.
37
119
/// </summary>
@@ -41,7 +123,7 @@ public static IQueryable Take([NotNull] this IQueryable source, int count)
41
123
public static IQueryable Skip ( [ NotNull ] this IQueryable source , int count )
42
124
{
43
125
Check . NotNull ( source , nameof ( source ) ) ;
44
- Check . Condition ( count , x => x >= 0 , nameof ( source ) ) ;
126
+ Check . Condition ( count , x => x >= 0 , nameof ( count ) ) ;
45
127
46
128
//no need to skip if count is zero
47
129
if ( count == 0 )
@@ -54,6 +136,14 @@ public static IQueryable Skip([NotNull] this IQueryable source, int count)
54
136
source . Expression , Expression . Constant ( count ) ) ) ;
55
137
}
56
138
139
+ public static IQueryable < TSource > Skip < TSource > ( [ NotNull ] this IQueryable < TSource > source , int count )
140
+ {
141
+ Check . NotNull ( source , nameof ( source ) ) ;
142
+ Check . Condition ( count , x => x >= 0 , nameof ( count ) ) ;
143
+
144
+ return count == 0 ? source : Queryable . Skip ( source , count ) ;
145
+ }
146
+
57
147
/// <summary>
58
148
/// Inverts the order of the elements in a sequence.
59
149
/// </summary>
@@ -83,6 +173,20 @@ public static bool Any([NotNull] this IQueryable source)
83
173
Expression . Call (
84
174
typeof ( Queryable ) , "Any" ,
85
175
new [ ] { source . ElementType } , source . Expression ) ) ;
176
+
177
+ //return (bool) source.Provider.Execute(
178
+ // Expression.Call(
179
+ // null,
180
+ // MethodInfoHelper.GetMethodInfoOf(() => BasicQueryable.Any(default(IQueryable))),
181
+ // new Expression[] { source.Expression }
182
+ // ));
183
+ }
184
+
185
+ public static bool Any < TSource > ( [ NotNull ] this IQueryable < TSource > source )
186
+ {
187
+ Check . NotNull ( source , nameof ( source ) ) ;
188
+
189
+ return Queryable . Any ( source ) ;
86
190
}
87
191
88
192
/// <summary>
@@ -100,6 +204,13 @@ public static int Count([NotNull] this IQueryable source)
100
204
new [ ] { source . ElementType } , source . Expression ) ) ;
101
205
}
102
206
207
+ public static int Count < TSource > ( [ NotNull ] this IQueryable < TSource > source )
208
+ {
209
+ Check . NotNull ( source , nameof ( source ) ) ;
210
+
211
+ return Queryable . Count ( source ) ;
212
+ }
213
+
103
214
/// <summary>
104
215
/// Computes the sum of a sequence of numeric values.
105
216
/// </summary>
@@ -137,6 +248,13 @@ public static dynamic Single([NotNull] this IQueryable source)
137
248
new [ ] { source . ElementType } , source . Expression ) ) ;
138
249
}
139
250
251
+ public static TSource Single < TSource > ( [ NotNull ] this IQueryable < TSource > source )
252
+ {
253
+ Check . NotNull ( source , nameof ( source ) ) ;
254
+
255
+ return Queryable . Single ( source ) ;
256
+ }
257
+
140
258
/// <summary>
141
259
/// Returns the only element of a sequence, or a default value if the sequence
142
260
/// is empty; this method throws an exception if there is more than one element
@@ -157,6 +275,13 @@ public static dynamic SingleOrDefault([NotNull] this IQueryable source)
157
275
new [ ] { source . ElementType } , source . Expression ) ) ;
158
276
}
159
277
278
+ public static TSource SingleOrDefault < TSource > ( [ NotNull ] this IQueryable < TSource > source )
279
+ {
280
+ Check . NotNull ( source , nameof ( source ) ) ;
281
+
282
+ return Queryable . SingleOrDefault ( source ) ;
283
+ }
284
+
160
285
/// <summary>
161
286
/// Returns the first element of a sequence.
162
287
/// </summary>
@@ -175,6 +300,13 @@ public static dynamic First([NotNull] this IQueryable source)
175
300
new [ ] { source . ElementType } , source . Expression ) ) ;
176
301
}
177
302
303
+ public static TSource First < TSource > ( [ NotNull ] this IQueryable < TSource > source )
304
+ {
305
+ Check . NotNull ( source , nameof ( source ) ) ;
306
+
307
+ return Queryable . First ( source ) ;
308
+ }
309
+
178
310
/// <summary>
179
311
/// Returns the first element of a sequence, or a default value if the sequence contains no elements.
180
312
/// </summary>
@@ -193,6 +325,13 @@ public static dynamic FirstOrDefault([NotNull] this IQueryable source)
193
325
new [ ] { source . ElementType } , source . Expression ) ) ;
194
326
}
195
327
328
+ public static TSource FirstOrDefault < TSource > ( [ NotNull ] this IQueryable < TSource > source )
329
+ {
330
+ Check . NotNull ( source , nameof ( source ) ) ;
331
+
332
+ return Queryable . FirstOrDefault ( source ) ;
333
+ }
334
+
196
335
/// <summary>
197
336
/// Returns the last element of a sequence.
198
337
/// </summary>
@@ -211,6 +350,13 @@ public static dynamic Last([NotNull] this IQueryable source)
211
350
new [ ] { source . ElementType } , source . Expression ) ) ;
212
351
}
213
352
353
+ public static TSource Last < TSource > ( [ NotNull ] this IQueryable < TSource > source )
354
+ {
355
+ Check . NotNull ( source , nameof ( source ) ) ;
356
+
357
+ return Queryable . Last ( source ) ;
358
+ }
359
+
214
360
/// <summary>
215
361
/// Returns the last element of a sequence, or a default value if the sequence contains no elements.
216
362
/// </summary>
@@ -229,6 +375,13 @@ public static dynamic LastOrDefault([NotNull] this IQueryable source)
229
375
new [ ] { source . ElementType } , source . Expression ) ) ;
230
376
}
231
377
378
+ public static TSource LastOrDefault < TSource > ( [ NotNull ] this IQueryable < TSource > source )
379
+ {
380
+ Check . NotNull ( source , nameof ( source ) ) ;
381
+
382
+ return Queryable . LastOrDefault ( source ) ;
383
+ }
384
+
232
385
#if NET35
233
386
/// <summary>
234
387
/// Returns the input typed as <see cref="IEnumerable{T}"/> of <see cref="object"/>./>
@@ -300,4 +453,4 @@ public static List<T> ToDynamicList<T>([NotNull] this IEnumerable source)
300
453
#endif
301
454
#endregion
302
455
}
303
- }
456
+ }
0 commit comments