Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
a-gubskiy committed Feb 14, 2023
2 parents ee3b97f + 805921a commit 6950d94
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/X.PagedList/PagedListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading;
using System.Threading.Tasks;


namespace X.PagedList;

/// <summary>
Expand Down Expand Up @@ -89,7 +88,7 @@ public static IEnumerable<IEnumerable<T>> Partition<T>(this IEnumerable<T> super
/// <seealso cref="PagedList{T}"/>
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pageNumber, int pageSize)
{
return new PagedList<T>(superset, pageNumber, pageSize);
return new PagedList<T>(superset ?? new List<T>(), pageNumber, pageSize);
}

/// <summary>
Expand All @@ -106,9 +105,10 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset, int pag
/// <seealso cref="PagedList{T}"/>
public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset)
{
int supersetSize = superset.Count();
int pageSize = supersetSize == 0 ? 1 : supersetSize;
return new PagedList<T>(superset, 1, pageSize);
var supersetSize = superset?.Count() ?? 0;
var pageSize = supersetSize == 0 ? 1 : supersetSize;

return new PagedList<T>(superset ?? new List<T>(), 1, pageSize);
}

/// <summary>
Expand All @@ -122,6 +122,7 @@ public static IPagedList<T> ToPagedList<T>(this IEnumerable<T> superset)
public static IPagedList<TResult> Select<TSource, TResult>(this IPagedList<TSource> source, Func<TSource, TResult> selector)
{
var subset = ((IEnumerable<TSource>)source).Select(selector);

return new PagedList<TResult>(source, subset);
}

Expand Down Expand Up @@ -223,6 +224,7 @@ public static async Task<int> CountAsync<T>(this IEnumerable<T> superset, Cancel
/// <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>
/// <param name="pageNumber">The one-based index of the subset of objects to be contained by this instance.</param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <param name="totalSetCount">The total size of set</param>
/// <param name="cancellationToken"></param>
/// <returns>
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
Expand Down Expand Up @@ -282,14 +284,15 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
/// The one-based index of the subset of objects to be contained by this instance.
/// </param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <param name="totalSetCount">The total size of set</param>
/// <returns>
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
/// about the collection of objects the subset was created from.
/// </returns>
/// <seealso cref="PagedList{T}"/>
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize, int? totalSetCount = null)
{
return await ToPagedListAsync(superset, pageNumber, pageSize, totalSetCount, CancellationToken.None);
return await ToPagedListAsync(AsQueryable(superset), pageNumber, pageSize, totalSetCount, CancellationToken.None);
}

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

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

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

/// <summary>
Expand All @@ -372,13 +378,24 @@ public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> s
/// defaulting to the first page.
/// </param>
/// <param name="pageSize">The maximum size of any individual subset.</param>
/// <param name="totalSetCount">The total size of set</param>
/// <returns>
/// A subset of this collection of objects that can be individually accessed by index and containing metadata
/// about the collection of objects the subset was created from.
/// </returns>
/// <seealso cref="PagedList{T}"/>
public static async Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int? pageNumber, int pageSize, int? totalSetCount = null)
{
return await ToPagedListAsync(superset.AsQueryable(), pageNumber ?? 1, pageSize, totalSetCount, CancellationToken.None);
return await ToPagedListAsync(AsQueryable(superset), pageNumber ?? 1, pageSize, totalSetCount, CancellationToken.None);
}

private static IQueryable<T> AsQueryable<T>(IQueryable<T> superset)
{
return superset ?? new EnumerableQuery<T>(new List<T>());
}

private static IQueryable<T> AsQueryable<T>(IEnumerable<T> superset)
{
return superset == null ? new EnumerableQuery<T>(new List<T>()) : superset.AsQueryable();
}
}

0 comments on commit 6950d94

Please sign in to comment.