Skip to content

Commit

Permalink
Merge pull request #307 from danm-de/feature/compat-support-ef6
Browse files Browse the repository at this point in the history
Added compatibility project for Entity Framework Version 6
  • Loading branch information
a-gubskiy authored Oct 24, 2024
2 parents 6a7ca61 + ce65068 commit bfb8d4d
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 1 deletion.
7 changes: 7 additions & 0 deletions X.PagedList.sln
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "X.PagedList.Mvc.Core", "src
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "X.PagedList.EF", "src\X.PagedList.EF\X.PagedList.EF.csproj", "{B72170CA-12E0-46E5-821C-FCE7E6F79736}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "X.PagedList.EF6", "src\X.PagedList.EF6\X.PagedList.EF6.csproj", "{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -54,6 +56,10 @@ Global
{B72170CA-12E0-46E5-821C-FCE7E6F79736}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B72170CA-12E0-46E5-821C-FCE7E6F79736}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B72170CA-12E0-46E5-821C-FCE7E6F79736}.Release|Any CPU.Build.0 = Release|Any CPU
{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -65,6 +71,7 @@ Global
{AD16A8D1-EAF0-4947-BCEC-A8B423B2F117} = {309A8FC8-4784-4D8D-903F-BD54EBB0F1D7}
{3B840A44-3150-4BB5-83DA-9B81D1FCB6BE} = {BDDADD09-D112-418E-8469-BC762EC09936}
{B72170CA-12E0-46E5-821C-FCE7E6F79736} = {BDDADD09-D112-418E-8469-BC762EC09936}
{BFFDE538-C911-46B4-ACE8-B4702ACCC6A5} = {BDDADD09-D112-418E-8469-BC762EC09936}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {1A82D446-6F26-48B2-8085-DFA5F87453FC}
Expand Down
99 changes: 99 additions & 0 deletions src/X.PagedList.EF6/PagedListExtensions.cs
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);
}
}
16 changes: 16 additions & 0 deletions src/X.PagedList.EF6/README.md
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.
29 changes: 29 additions & 0 deletions src/X.PagedList.EF6/X.PagedList.EF6.csproj
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 added src/X.PagedList.EF6/xpagedlist.snk
Binary file not shown.
2 changes: 1 addition & 1 deletion src/X.PagedList/X.PagedList.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Library for easily paging through any IEnumerable/IQueryable in .NET</Description>

<LangVersion>default</LangVersion>
<TargetFrameworks>net6.0;net7.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>net481;net6.0;net7.0;net8.0;netstandard2.0;netstandard2.1</TargetFrameworks>

<PackageTags>paging pagedlist paged list</PackageTags>
</PropertyGroup>
Expand Down

0 comments on commit bfb8d4d

Please sign in to comment.