Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions EntityFrameworkPaginate/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
13 changes: 13 additions & 0 deletions EntityFrameworkPaginate/EntityFrameworkPaginate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="EntityFramework.SqlServer, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
<HintPath>..\packages\EntityFramework.6.1.3\lib\net45\EntityFramework.SqlServer.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
Expand All @@ -49,6 +58,10 @@
<Compile Include="Sort.cs" />
<Compile Include="Sorts.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
26 changes: 15 additions & 11 deletions EntityFrameworkPaginate/PaginateService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Data.Entity;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Threading.Tasks;

namespace EntityFrameworkPaginate
{
Expand All @@ -14,14 +17,14 @@ public static class PaginateService
/// <param name="pageNumber">The page no. which needs to be fetched.</param>
/// <param name="pageSize">The number or records expected in the page.</param>
/// <returns>A Page object with filtered data for the given page number and page size.</returns>
public static Page<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize)
public static async Task<Page<T>> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize)
{
var result = new Page<T>
Page<T> result = new Page<T>
{
CurrentPage = pageNumber,
PageSize = pageSize,
RecordCount = query.Count(),
Results = query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList()
RecordCount = await query.CountAsync(),
Results = await query.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToListAsync()
};
result.PageCount = (int)Math.Ceiling((double)result.RecordCount / pageSize);
return result;
Expand All @@ -36,9 +39,10 @@ public static Page<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int
/// <param name="pageSize">The number or records expected in the page.</param>
/// <param name="sorts">Conditional sorts.</param>
/// <returns>A Page object with filtered data for the given page number and page size.</returns>
public static Page<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize, Sorts<T> sorts)
public static async Task<Page<T>> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize, Sorts<T> sorts)
{
return query.ApplySort(sorts).Paginate(pageNumber, pageSize);
IQueryable<T> result = await query.ApplySort(sorts);
return await result.Paginate(pageNumber, pageSize);
}

/// <summary>
Expand All @@ -51,21 +55,21 @@ public static Page<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int
/// <param name="sorts">Conditional sorts.</param>
/// <param name="filters">Conditional filters.</param>
/// <returns>A Page object with filtered data for the given page number and page size.</returns>
public static Page<T> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize, Sorts<T> sorts, Filters<T> filters)
public static async Task<Page<T>> Paginate<T>(this IQueryable<T> query, int pageNumber, int pageSize, Sorts<T> sorts, Filters<T> filters)
{
return query.ApplyFilter(filters).Paginate(pageNumber, pageSize, sorts);
return await query.ApplyFilter(filters).Paginate(pageNumber, pageSize, sorts);
}

private static IQueryable<T> ApplyFilter<T>(this IQueryable<T> query, Filters<T> filters)
{
return !filters.IsValid() ? query : filters.Get().Aggregate(query, (current, filter) => current.Where(filter.Expression));
}

private static IQueryable<T> ApplySort<T>(this IQueryable<T> query, Sorts<T> sorts)
private static async Task<IQueryable<T>> ApplySort<T>(this IQueryable<T> query, Sorts<T> sorts)
{
if (!sorts.IsValid()) return query;
var sort = sorts.Get();
return Sorts<T>.ApplySort(query, sort);
dynamic sort = sorts.Get();
return await Sorts<T>.ApplySort(query, sort);
}
}
}
4 changes: 4 additions & 0 deletions EntityFrameworkPaginate/packages.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="net45" />
</packages>