DeveloperPartners.SortingFiltering
2.1.2
dotnet add package DeveloperPartners.SortingFiltering --version 2.1.2
NuGet\Install-Package DeveloperPartners.SortingFiltering -Version 2.1.2
<PackageReference Include="DeveloperPartners.SortingFiltering" Version="2.1.2" />
<PackageVersion Include="DeveloperPartners.SortingFiltering" Version="2.1.2" />
<PackageReference Include="DeveloperPartners.SortingFiltering" />
paket add DeveloperPartners.SortingFiltering --version 2.1.2
#r "nuget: DeveloperPartners.SortingFiltering, 2.1.2"
#:package DeveloperPartners.SortingFiltering@2.1.2
#addin nuget:?package=DeveloperPartners.SortingFiltering&version=2.1.2
#tool nuget:?package=DeveloperPartners.SortingFiltering&version=2.1.2
DeveloperPartners.SortingFiltering
The DeveloperPartners.SortingFiltering library allows dynamically building Entity Framework queries for sorting, filtering, and pagination.
Let's assume you have an Entity Framework model called Product with the following properties:
public class Product
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public DateTime DateCreated { get; set; }
public decimal UnitPrice { get; set; }
}
You can build dynamic Entity Framework queries using our extension methods for sorting, filtering, and paginating the Products table rows with the your model properties.
The dynamic queries for sorting and filtering are built using the Query class. The Query class has Filter and Sort properties. The Filter property is used for dynamically filtering data. The Sort property is used for dynamically sorting data.
public class Query
{
public QueryFilter Filter { get; set; }
public IDictionary<string, SortOperator> Sort { get; set; }
}
Sorting
The Query.Sort property is a dictionary where the key of the dictionary is the property name that you want to sort by and the value is an enum representing whether the sort direction should be in ascending or descending order.
using Microsoft.EntityFrameworkCore;
using DeveloperPartners.SortingFiltering.EntityFrameworkCore;
using (var context = new AppDbContext())
{
var query = new Query();
query.Sort.Add("DateCreated", SortOperator.Desc);
query.Sort.Add("Name", SortOperator.Asc);
var products = await context
.Products
// The OrderBy method creates dynamic sorting by DateCreated descending order, then by Name ascending order.
.OrderBy(query.Sort)
.ToListAsync();
}
Pagination
Pagination is done by using the PageInfo class. You can use the PageNumber and PageSize properties of the PageInfo class to specify which page should be queries and how many records should each page contain.
using Microsoft.EntityFrameworkCore;
using DeveloperPartners.SortingFiltering.EntityFrameworkCore;
using (var context = new AppDbContext())
{
var pageInfo = new PageInfo
{
PageNumber = 1,
PageSize = 20
};
var firstPageProducts = await context
.Products
// Data must be sorted for pagination.
.OrderBy(product => product.Id)
// The Paginate method takes the first page data with 20 records per page.
.Paginate(pageInfo)
.ToListAsync();
var firstPage = firstPageProducts.ToPagedData(pageInfo);
Console.WriteLine(pageInfo.PageNumber);
Console.WriteLine(pageInfo.PageSize);
Console.WriteLine(pageInfo.TotalPages);
foreach (var product in firstPage.Data)
{
// Use products here.
}
}
For being able to paginate, the data must first be sorted. You can use the regular Entity Framework OrderBy method to paginate sort by the properties you know at design time or you can use Query.Sort for dynamically sorting and dynamically paginating the sorted data:
using Microsoft.EntityFrameworkCore;
using DeveloperPartners.SortingFiltering.EntityFrameworkCore;
using (var context = new AppDbContext())
{
var pageInfo = new PageInfo
{
PageNumber = 1,
PageSize = 20
};
var query = new Query();
query.Sort.Add("DateCreated", SortOperator.Desc);
query.Sort.Add("Name", SortOperator.Asc);
var firstPageProducts = context
.Products
// Data must be sorted for pagination. Use Query.Sort for dynamically sorting data before pagination.
.OrderBy(query.Sort)
// The Paginate method takes the first page data with 20 records per page.
.Paginate(pageInfo)
.ToListAsync();
var firstPage = firstPageProducts.ToPagedData(pageInfo);
Console.WriteLine(pageInfo.PageNumber);
Console.WriteLine(pageInfo.PageSize);
Console.WriteLine(pageInfo.TotalPages);
foreach (var product in firstPage.Data)
{
// Use products here.
}
}
Filtering
The Query.Filter property is a lit of QueryProperty objects. You can have multiple filtering conditions by adding multiple QueryProperty objects to the Query.Filter list.
using Microsoft.EntityFrameworkCore;
using DeveloperPartners.SortingFiltering.EntityFrameworkCore;
using (var context = new AppDbContext())
{
var query = new Query();
query.Filter.AddRange(new []
{
new QueryProperty
{
ColumnName = "Name",
Value = "iPhone",
ComparisonOperator = ComparisonOperator.Ct
},
new QueryProperty
{
ColumnName = "UnitPrice",
Value = "1000",
ComparisonOperator = ComparisonOperator.Lte
}
});
var products = await context
.Products
.Where(query.Filter)
.ToListAsync();
}
The query above returns products where Name contains the word "iPhone" and UnitPrice is less than or equal to 1,000.
Using as Query String Parameters
The real power of DeveloperPartners.SortingFiltering comes when using PageInfo and Query as query string parameters of API endpoints. Let's take a look at the following endpoint:
public async Task<IActionResult> GetAllProductsAsync([FromQuery] PageInfo pageInfo, [FromQuery] Query query)
{
var products = await _context
.Products
.Where(query.Filter)
.OrderBy(query.Sort)
.Pageinate(pageInfo)
.ToListAsync();
var pagedData = products.ToPagedData(pageInfo);
return Ok(pagedData);
}
You can call the endpoint above with the following URL and query string parameters:
/api/products?pageNumber=2&pageSize=25&s[DateCreated]=Desc&s[Name]=Asc&q[0].col=Name&q[0].op=Ct&q[0].val=iPhone
The query string above translates to "Get products where Name contains "iPhone". Sort by DateCreated in descending order, then by Name in ascending order. Paginate the data with page size of 25 and get the data of the second page."
Credits
Developer Partners, Inc.
| Product | Versions 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. 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. |
-
net8.0
- AutoMapper (>= 12.0.0)
- Microsoft.AspNetCore.Identity.EntityFrameworkCore (>= 8.0.0)
- Newtonsoft.Json (>= 13.0.1)
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 |
|---|---|---|
| 2.1.2 | 225 | 12/25/2025 |
| 2.1.0 | 1,232 | 12/5/2024 |
| 2.0.1 | 238 | 11/21/2024 |
| 2.0.0 | 214 | 11/15/2024 |
| 1.1.0 | 218 | 7/29/2024 |
| 1.0.13 | 399 | 7/11/2024 |
| 1.0.12 | 304 | 6/2/2024 |
| 1.0.10 | 511 | 8/13/2023 |
| 1.0.9 | 253 | 7/18/2023 |
| 1.0.8 | 392 | 6/14/2023 |
| 1.0.7 | 360 | 5/3/2023 |
| 1.0.6 | 588 | 1/26/2023 |
| 1.0.5 | 374 | 1/26/2023 |
| 1.0.4 | 394 | 1/26/2023 |
| 1.0.3 | 388 | 1/21/2023 |
| 1.0.2 | 381 | 1/21/2023 |
| 1.0.1 | 470 | 11/19/2022 |