Pitman.OrderBy 1.0.2

dotnet add package Pitman.OrderBy --version 1.0.2
                    
NuGet\Install-Package Pitman.OrderBy -Version 1.0.2
                    
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="Pitman.OrderBy" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pitman.OrderBy" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="Pitman.OrderBy" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Pitman.OrderBy --version 1.0.2
                    
#r "nuget: Pitman.OrderBy, 1.0.2"
                    
#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.
#:package Pitman.OrderBy@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Pitman.OrderBy&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=Pitman.OrderBy&version=1.0.2
                    
Install as a Cake Tool

Pitman.OrderBy

Pitman.OrderBy is a .NET library that provides an implementation of an OrderByQuery class that enables you to sort an IQueryable collection of entities dynamically based on a field passed as a query parameter. With "OrderBy", you can automatically get the field of order by from your endpoint and apply it to an IQueryable.

Usage You can use "OrderBy" in your .NET project by installing the "OrderByQuery" NuGet package.

Installation You can install "OrderByQuery" by running the following command in the Package Manager Console:

Install-Package Pitman.OrderBy

Example

Here's an example of how to use "OrderByQuery" to sort an IQueryable collection of entities based on a field passed as a query parameter.

Using OrderByQuery

var orderBy = new OrderByQuery("age:asc");

var list = new List<Person>
{
    new Person() { Id = 3, Name = "C", Age = 3 },
    new Person() { Id = 4, Name = "D", Age = 4 },
    new Person() { Id = 1, Name = "A", Age = 1 },
    new Person() { Id = 2, Name = "B", Age = 2 },
    new Person() { Id = 5, Name = "E", Age = 5 }
};

var orderedList = orderBy.ApplyOrder(list.AsQueryable());

Using Asp.net

On a controller use [FromQuery] OrderBy and apply the order on an IQueryable list. In the url add the parameter order with the value of {propertyName}:{direction}. Direction can be either asc or desc.

Example of an url sorting the by temperatureC as property name:

https://localhost:7201/weatherforecast?order=temperatureC:asc
or
https://localhost:7201/weatherforecast?order=temperatureC:desc
Full Example
[HttpGet]
public IEnumerable<WeatherForecast> Get([FromQuery] OrderBy orderBy) // receive order from url parameters
{
    return Results(orderBy);
}

private static IEnumerable<WeatherForecast> Results(IOrderBy orderBy)
{
    var queryable =  Enumerable.Range(1, 5).Select(index => new WeatherForecast
    {
        Date = DateTime.Now.AddDays(index),
        TemperatureC = Random.Shared.Next(-20, 55),
        Summary = Summaries[Random.Shared.Next(Summaries.Length)]
    })
    .AsQueryable();

    queryable = orderBy.ApplyOrder(queryable); // apply the order on the IQueryable

    return queryable;

}

Contributing

Contributions to "OrderByQuery" are welcome and encouraged! If you find a bug or have a feature request, please create an issue in the repository. If you'd like to contribute code to the project, please open a pull request with your changes.

License

"OrderBy" is released under the MIT License. See LICENSE for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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
1.0.2 745 2/28/2023