Skip to content
Merged
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
40 changes: 40 additions & 0 deletions src/MADE.Data.EFCore/Extensions/QueryableExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
namespace MADE.Data.EFCore.Extensions
{
using System;
using System.Linq;
using Z.EntityFramework.Plus;

/// <summary>
/// Defines a collection of extensions for Entity Framework queries.
/// </summary>
public static class QueryableExtensions
{
/// <summary>
/// Skips and takes a subset of a data query based on the specified current page and page size requested.
/// </summary>
/// <typeparam name="T">The type of entity being queried.</typeparam>
/// <param name="query">The current query.</param>
/// <param name="page">The current page being requested.</param>
/// <param name="pageSize">The size of the page being requested.</param>
/// <returns>The paginated query.</returns>
public static IQueryable<T> Page<T>(this IQueryable<T> query, int page, int pageSize)
{
return query.Skip((page - 1) * pageSize).Take(pageSize);
}

/// <summary>
/// Orders the query results by the specified property name from the entity with the option for order by ascending or descending.
/// </summary>
/// <typeparam name="T">The type of entity being ordered.</typeparam>
/// <param name="query">The query to order.</param>
/// <param name="sortName">The property/column name to sort on for the entity.</param>
/// <param name="sortDesc">A value indicating whether to sort descending.</param>
/// <returns>The ordered query.</returns>
public static IQueryable<T> OrderBy<T>(this IQueryable<T> query, string sortName, bool sortDesc)
{
return string.IsNullOrWhiteSpace(sortName)
? query
: (!sortDesc ? query.AddOrAppendOrderBy(sortName) : query.AddOrAppendOrderByDescending(sortName));
}
}
}
3 changes: 3 additions & 0 deletions src/MADE.Data.EFCore/MADE.Data.EFCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@
- DbContextExtensions for extending DbContext objects with helper methods, such as updating an item asynchronously and removing items matching a specified criteria.
- EntityBase for providing a base definition for an entity within a DbContext, including a primary key, created and updated dates.
- UtcDateTimeConverter for ensuring that DateTime values are stored and retrieving in UTC format.
- QueryableExtensions for extending IQueryable objects with helper methods, such as filtering, ordering, and paging.
</Description>
<PackageTags>MADE EFCore EntityFramework</PackageTags>

</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.4" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="6.13.18" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)'=='net5.0'">
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.16" />
<PackageReference Include="Z.EntityFramework.Plus.EFCore" Version="5.13.18" />
</ItemGroup>

</Project>