6
6
using System . Threading ;
7
7
using System . Threading . Tasks ;
8
8
9
-
10
9
namespace X . PagedList ;
11
10
12
11
/// <summary>
@@ -89,7 +88,7 @@ public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> super
89
88
/// <seealso cref="PagedList{T}"/>
90
89
public static IPagedList < T > ToPagedList < T > ( this IEnumerable < T > superset , int pageNumber , int pageSize )
91
90
{
92
- return new PagedList < T > ( superset , pageNumber , pageSize ) ;
91
+ return new PagedList < T > ( superset ?? new List < T > ( ) , pageNumber , pageSize ) ;
93
92
}
94
93
95
94
/// <summary>
@@ -106,9 +105,10 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pag
106
105
/// <seealso cref="PagedList{T}"/>
107
106
public static IPagedList < T > ToPagedList < T > ( this IEnumerable < T > superset )
108
107
{
109
- int supersetSize = superset . Count ( ) ;
110
- int pageSize = supersetSize == 0 ? 1 : supersetSize ;
111
- return new PagedList < T > ( superset , 1 , pageSize ) ;
108
+ var supersetSize = superset ? . Count ( ) ?? 0 ;
109
+ var pageSize = supersetSize == 0 ? 1 : supersetSize ;
110
+
111
+ return new PagedList < T > ( superset ?? new List < T > ( ) , 1 , pageSize ) ;
112
112
}
113
113
114
114
/// <summary>
@@ -122,6 +122,7 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset)
122
122
public static IPagedList < TResult > Select < TSource , TResult > ( this IPagedList < TSource > source , Func < TSource , TResult > selector )
123
123
{
124
124
var subset = ( ( IEnumerable < TSource > ) source ) . Select ( selector ) ;
125
+
125
126
return new PagedList < TResult > ( source , subset ) ;
126
127
}
127
128
@@ -223,6 +224,7 @@ public static async Task<int> CountAsync<T>(this IEnumerable<T> superset, Cancel
223
224
/// <param name="superset">The collection of objects to be divided into subsets. If the collection implements <see cref="IQueryable{T}"/>, it will be treated as such.</param>
224
225
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
225
226
/// <param name="pageSize">The maximum size of any individual subset.</param>
227
+ /// <param name="totalSetCount">The total size of set</param>
226
228
/// <param name="cancellationToken"></param>
227
229
/// <returns>
228
230
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
@@ -282,14 +284,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
282
284
/// The one-based index of the subset of objects to be contained by this instance.
283
285
/// </param>
284
286
/// <param name="pageSize">The maximum size of any individual subset.</param>
287
+ /// <param name="totalSetCount">The total size of set</param>
285
288
/// <returns>
286
289
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
287
290
/// about the collection of objects the subset was created from.
288
291
/// </returns>
289
292
/// <seealso cref="PagedList{T}"/>
290
293
public static async Task < IPagedList < T > > ToPagedListAsync < T > ( this IQueryable < T > superset , int pageNumber , int pageSize , int ? totalSetCount = null )
291
294
{
292
- return await ToPagedListAsync ( superset , pageNumber , pageSize , totalSetCount , CancellationToken . None ) ;
295
+ return await ToPagedListAsync ( AsQueryable ( superset ) , pageNumber , pageSize , totalSetCount , CancellationToken . None ) ;
293
296
}
294
297
295
298
/// <summary>
@@ -306,14 +309,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
306
309
/// </param>
307
310
/// <param name="pageSize">The maximum size of any individual subset.</param>
308
311
/// <param name="cancellationToken"></param>
312
+ /// <param name="totalSetCount">The total size of set</param>
309
313
/// <returns>
310
314
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
311
315
/// about the collection of objects the subset was created from.
312
316
/// </returns>
313
317
/// <seealso cref="PagedList{T}"/>
314
318
public static async Task < IPagedList < T > > ToPagedListAsync < T > ( this IEnumerable < T > superset , int pageNumber , int pageSize , CancellationToken cancellationToken , int ? totalSetCount = null )
315
319
{
316
- return await ToPagedListAsync ( superset . AsQueryable ( ) , pageNumber , pageSize , totalSetCount , cancellationToken ) ;
320
+ return await ToPagedListAsync ( AsQueryable ( superset ) , pageNumber , pageSize , totalSetCount , cancellationToken ) ;
317
321
}
318
322
319
323
/// <summary>
@@ -329,14 +333,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T>
329
333
/// The one-based index of the subset of objects to be contained by this instance.
330
334
/// </param>
331
335
/// <param name="pageSize">The maximum size of any individual subset.</param>
336
+ /// <param name="totalSetCount">The total size of set</param>
332
337
/// <returns>
333
338
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
334
339
/// about the collection of objects the subset was created from.
335
340
/// </returns>
336
341
/// <seealso cref="PagedList{T}"/>
337
342
public static async Task < IPagedList < T > > ToPagedListAsync < T > ( this IEnumerable < T > superset , int pageNumber , int pageSize , int ? totalSetCount = null )
338
343
{
339
- return await ToPagedListAsync ( superset . AsQueryable ( ) , pageNumber , pageSize , totalSetCount , CancellationToken . None ) ;
344
+ return await ToPagedListAsync ( AsQueryable ( superset ) , pageNumber , pageSize , totalSetCount , CancellationToken . None ) ;
340
345
}
341
346
342
347
/// <summary>
@@ -350,14 +355,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T>
350
355
/// </param>
351
356
/// <param name="pageSize">The maximum size of any individual subset.</param>
352
357
/// <param name="cancellationToken"></param>
358
+ /// <param name="totalSetCount">The total size of set</param>
353
359
/// <returns>
354
360
/// A subset of this collection of objects that can be individually accessed by index and containing
355
361
/// metadata about the collection of objects the subset was created from.
356
362
/// </returns>
357
363
/// <seealso cref="PagedList{T}"/>
358
364
public static async Task < IPagedList < T > > ToPagedListAsync < T > ( this IQueryable < T > superset , int ? pageNumber , int pageSize , CancellationToken cancellationToken , int ? totalSetCount = null )
359
365
{
360
- return await ToPagedListAsync ( superset . AsQueryable ( ) , pageNumber ?? 1 , pageSize , totalSetCount , cancellationToken ) ;
366
+ return await ToPagedListAsync ( AsQueryable ( superset ) , pageNumber ?? 1 , pageSize , totalSetCount , cancellationToken ) ;
361
367
}
362
368
363
369
/// <summary>
@@ -372,13 +378,24 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
372
378
/// defaulting to the first page.
373
379
/// </param>
374
380
/// <param name="pageSize">The maximum size of any individual subset.</param>
381
+ /// <param name="totalSetCount">The total size of set</param>
375
382
/// <returns>
376
383
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
377
384
/// about the collection of objects the subset was created from.
378
385
/// </returns>
379
386
/// <seealso cref="PagedList{T}"/>
380
387
public static async Task < IPagedList < T > > ToPagedListAsync < T > ( this IQueryable < T > superset , int ? pageNumber , int pageSize , int ? totalSetCount = null )
381
388
{
382
- return await ToPagedListAsync ( superset . AsQueryable ( ) , pageNumber ?? 1 , pageSize , totalSetCount , CancellationToken . None ) ;
389
+ return await ToPagedListAsync ( AsQueryable ( superset ) , pageNumber ?? 1 , pageSize , totalSetCount , CancellationToken . None ) ;
390
+ }
391
+
392
+ private static IQueryable < T > AsQueryable < T > ( IQueryable < T > superset )
393
+ {
394
+ return superset ?? new EnumerableQuery < T > ( new List < T > ( ) ) ;
395
+ }
396
+
397
+ private static IQueryable < T > AsQueryable < T > ( IEnumerable < T > superset )
398
+ {
399
+ return superset == null ? new EnumerableQuery < T > ( new List < T > ( ) ) : superset . AsQueryable ( ) ;
383
400
}
384
401
}
0 commit comments