Skip to content

Commit 6950d94

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents ee3b97f + 805921a commit 6950d94

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/X.PagedList/PagedListExtensions.cs

+27-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88

9-
109
namespace X.PagedList;
1110

1211
/// <summary>
@@ -89,7 +88,7 @@ public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> super
8988
/// <seealso cref="PagedList{T}"/>
9089
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pageNumber, int pageSize)
9190
{
92-
return new PagedList<T>(superset, pageNumber, pageSize);
91+
return new PagedList<T>(superset ?? new List<T>(), pageNumber, pageSize);
9392
}
9493

9594
/// <summary>
@@ -106,9 +105,10 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pag
106105
/// <seealso cref="PagedList{T}"/>
107106
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset)
108107
{
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);
112112
}
113113

114114
/// <summary>
@@ -122,6 +122,7 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset)
122122
public static IPagedList<TResult> Select<TSource, TResult>(this IPagedList<TSource> source, Func<TSource, TResult> selector)
123123
{
124124
var subset = ((IEnumerable<TSource>)source).Select(selector);
125+
125126
return new PagedList<TResult>(source, subset);
126127
}
127128

@@ -223,6 +224,7 @@ public static async Task<int> CountAsync<T>(this IEnumerable<T> superset, Cancel
223224
/// <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>
224225
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
225226
/// <param name="pageSize">The maximum size of any individual subset.</param>
227+
/// <param name="totalSetCount">The total size of set</param>
226228
/// <param name="cancellationToken"></param>
227229
/// <returns>
228230
/// 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
282284
/// The one-based index of the subset of objects to be contained by this instance.
283285
/// </param>
284286
/// <param name="pageSize">The maximum size of any individual subset.</param>
287+
/// <param name="totalSetCount">The total size of set</param>
285288
/// <returns>
286289
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
287290
/// about the collection of objects the subset was created from.
288291
/// </returns>
289292
/// <seealso cref="PagedList{T}"/>
290293
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize, int? totalSetCount = null)
291294
{
292-
return await ToPagedListAsync(superset, pageNumber, pageSize, totalSetCount, CancellationToken.None);
295+
return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None);
293296
}
294297

295298
/// <summary>
@@ -306,14 +309,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
306309
/// </param>
307310
/// <param name="pageSize">The maximum size of any individual subset.</param>
308311
/// <param name="cancellationToken"></param>
312+
/// <param name="totalSetCount">The total size of set</param>
309313
/// <returns>
310314
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
311315
/// about the collection of objects the subset was created from.
312316
/// </returns>
313317
/// <seealso cref="PagedList{T}"/>
314318
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T> superset, int pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null)
315319
{
316-
return await ToPagedListAsync(superset.AsQueryable(), pageNumber, pageSize, totalSetCount, cancellationToken);
320+
return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, cancellationToken);
317321
}
318322

319323
/// <summary>
@@ -329,14 +333,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T>
329333
/// The one-based index of the subset of objects to be contained by this instance.
330334
/// </param>
331335
/// <param name="pageSize">The maximum size of any individual subset.</param>
336+
/// <param name="totalSetCount">The total size of set</param>
332337
/// <returns>
333338
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
334339
/// about the collection of objects the subset was created from.
335340
/// </returns>
336341
/// <seealso cref="PagedList{T}"/>
337342
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T> superset, int pageNumber, int pageSize, int? totalSetCount = null)
338343
{
339-
return await ToPagedListAsync(superset.AsQueryable(), pageNumber, pageSize, totalSetCount, CancellationToken.None);
344+
return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None);
340345
}
341346

342347
/// <summary>
@@ -350,14 +355,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IEnumerable<T>
350355
/// </param>
351356
/// <param name="pageSize">The maximum size of any individual subset.</param>
352357
/// <param name="cancellationToken"></param>
358+
/// <param name="totalSetCount">The total size of set</param>
353359
/// <returns>
354360
/// A subset of this collection of objects that can be individually accessed by index and containing
355361
/// metadata about the collection of objects the subset was created from.
356362
/// </returns>
357363
/// <seealso cref="PagedList{T}"/>
358364
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int? pageNumber, int pageSize, CancellationToken cancellationToken, int? totalSetCount = null)
359365
{
360-
return await ToPagedListAsync(superset.AsQueryable(), pageNumber ?? 1, pageSize, totalSetCount, cancellationToken);
366+
return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, cancellationToken);
361367
}
362368

363369
/// <summary>
@@ -372,13 +378,24 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
372378
/// defaulting to the first page.
373379
/// </param>
374380
/// <param name="pageSize">The maximum size of any individual subset.</param>
381+
/// <param name="totalSetCount">The total size of set</param>
375382
/// <returns>
376383
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
377384
/// about the collection of objects the subset was created from.
378385
/// </returns>
379386
/// <seealso cref="PagedList{T}"/>
380387
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int? pageNumber, int pageSize, int? totalSetCount = null)
381388
{
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();
383400
}
384401
}

0 commit comments

Comments
 (0)