-
-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #307 from danm-de/feature/compat-support-ef6
Added compatibility project for Entity Framework Version 6
- Loading branch information
Showing
6 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Data.Entity; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using JetBrains.Annotations; | ||
|
||
namespace X.PagedList.EF; | ||
|
||
/// <summary> | ||
/// EntityFramework extension methods designed to simplify the creation of instances of <see cref="PagedList{T}"/>. | ||
/// </summary> | ||
[PublicAPI] | ||
public static class PagedListExtensions | ||
{ | ||
/// <summary> | ||
/// Async creates 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. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object the collection should contain.</typeparam> | ||
/// <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 | ||
/// 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, CancellationToken cancellationToken) | ||
{ | ||
if (superset == null) | ||
{ | ||
throw new ArgumentNullException(nameof(superset)); | ||
} | ||
|
||
if (pageNumber < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException($"pageNumber = {pageNumber}. PageNumber cannot be below 1."); | ||
} | ||
|
||
if (pageSize < 1) | ||
{ | ||
throw new ArgumentOutOfRangeException($"pageSize = {pageSize}. PageSize cannot be less than 1."); | ||
} | ||
|
||
List<T> subset; | ||
int totalCount; | ||
|
||
if (totalSetCount.HasValue) | ||
{ | ||
totalCount = totalSetCount.Value; | ||
} | ||
else | ||
{ | ||
totalCount = await superset | ||
.CountAsync(cancellationToken: cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
|
||
if (totalCount > 0) | ||
{ | ||
int skip = (pageNumber - 1) * pageSize; | ||
|
||
subset = await superset | ||
.Skip(skip) | ||
.Take(pageSize) | ||
.ToListAsync(cancellationToken) | ||
.ConfigureAwait(false); | ||
} | ||
else | ||
{ | ||
subset = new List<T>(); | ||
} | ||
|
||
return new StaticPagedList<T>(subset, pageNumber, pageSize, totalCount); | ||
} | ||
|
||
/// <summary> | ||
/// Async creates 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. | ||
/// </summary> | ||
/// <typeparam name="T">The type of object the collection should contain.</typeparam> | ||
/// <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> | ||
/// <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 Task<IPagedList<T>> ToPagedListAsync<T>(this IQueryable<T> superset, int pageNumber, int pageSize, int? totalSetCount = null) | ||
{ | ||
return ToPagedListAsync(superset, pageNumber, pageSize, totalSetCount, CancellationToken.None); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# X.Extensions.PagedList.EF | ||
|
||
[![NuGet Version](http://img.shields.io/nuget/v/X.PagedList.EF6.svg?style=flat)](https://www.nuget.org/packages/X.PagedList.EF6/) | ||
[![Twitter URL](https://img.shields.io/twitter/url/https/x.com/andrew_gubskiy.svg?style=social&label=Follow%20me!)](https://x.com/intent/user?screen_name=andrew_gubskiy) | ||
|
||
|
||
## What is this? | ||
The X.PagedList.EF6 library provides Entity Framework (Version 6) extensions for the X.PagedList library, enabling easier | ||
paging through data collections within Entity Framework contexts. This extension facilitates the creation of paged | ||
lists from IQueryable collections, streamlining the process of managing large datasets in .NET applications. | ||
|
||
## How to use | ||
You can find all information about how to use X.PagedList libraries in [Wiki](https://github.com/dncuug/X.PagedList/wiki) | ||
|
||
## Get a digital subscription for project news | ||
[Subscribe](https://x.com/intent/user?screen_name=andrew_gubskiy) to my X to keep up-to-date with project news and receive announcements. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Title>X.PagedList.EF6</Title> | ||
<Description>EF6 (legacy) extensions for X.PagedList library</Description> | ||
<LangVersion>default</LangVersion> | ||
<RootNamespace>X.PagedList.EF</RootNamespace> | ||
<TargetFrameworks>net481;netstandard2.1;</TargetFrameworks> | ||
<PackageTags>paging pagedlist paged list entity framework ef6</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="README.md" Pack="true" PackagePath=""/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../LICENSE.md" Pack="true" PackagePath=""/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="EntityFramework" Version="[6.5.1,7.0.0)" /> | ||
<PackageReference Include="JetBrains.Annotations" Version="2024.2.0" PrivateAssets="all"/> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\X.PagedList\X.PagedList.csproj"/> | ||
</ItemGroup> | ||
|
||
</Project> |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters