EntityFrameworkCore.Order 8.0.0

dotnet add package EntityFrameworkCore.Order --version 8.0.0
NuGet\Install-Package EntityFrameworkCore.Order -Version 8.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.Order" Version="8.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.Order --version 8.0.0
#r "nuget: EntityFrameworkCore.Order, 8.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.Order as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.Order&version=8.0.0

// Install EntityFrameworkCore.Order as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.Order&version=8.0.0

EntityFrameworkCore.Order Nuget Package

Dependency

.Net 8

Description

This nuget package allow you to sort the list by property name. If you want to short the list by OrderBy or OrderByDescy you have to send PropertyName and Reverse parameter

Parameters

public sealed record GetAllBoomQuery(
    bool IsOrderTable = false, //Optional
    string ColumnName = "",
    bool Reverse = false) : IRequest<List<Boom>>;

Usage

        IQueryable<Boom> list = _boomRepository
            .AsQueryable();
        if (request.IsOrderTable) //Optional
        {
            list = list.RequestOrderBy(request.ColumnName, request.Reverse);
        }
        else
        {
            list = list.RequestOrderBy("CreatedDate", true);
        }

        return await list.ToListAsync(request.PageNumber, request.PageSize, cancellationToken);

Source Code

public static class QueryableExtensions
{
    public static IOrderedQueryable<T> RequestOrderBy<T>(this IQueryable<T> source, string propertyName, bool descending)
    {
        // Retrieve the property by reflection.
        var property = typeof(T).GetProperty(propertyName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

        // If the property doesn't exist, throw an exception.
        if (property == null)
            throw new ArgumentException($"Property '{propertyName}' not found on type '{typeof(T)}'.");

        // Create the selector.
        var parameter = Expression.Parameter(typeof(T));
        var selector = Expression.Lambda<Func<T, object>>(Expression.Convert(Expression.Property(parameter, property), typeof(object)), parameter);

        // Construct the appropriate sort method and invoke it.
        var sortMethod = descending ? "OrderByDescending" : "OrderBy";
        var method = typeof(Queryable).GetMethods()
            .Where(x => x.Name == sortMethod && x.GetParameters().Length == 2)
            .Single()
            .MakeGenericMethod(typeof(T), typeof(object));

        return (IOrderedQueryable<T>)method.Invoke(null, new object[] { source, selector });
    }
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0 199 11/15/2023
7.0.5 117 9/4/2023
1.0.0 128 8/23/2023