QueryFilters 1.0.3

dotnet add package QueryFilters --version 1.0.3
                    
NuGet\Install-Package QueryFilters -Version 1.0.3
                    
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="QueryFilters" Version="1.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QueryFilters" Version="1.0.3" />
                    
Directory.Packages.props
<PackageReference Include="QueryFilters" />
                    
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 QueryFilters --version 1.0.3
                    
#r "nuget: QueryFilters, 1.0.3"
                    
#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 QueryFilters@1.0.3
                    
#: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=QueryFilters&version=1.0.3
                    
Install as a Cake Addin
#tool nuget:?package=QueryFilters&version=1.0.3
                    
Install as a Cake Tool

Queryfilters

This notebook is a simple example of how to use this package to make filtering data a lot easier when querying a database through an api.

The package allows easy filtering, sorting and pagination of data from a database.

Implementation

To use the package, you need to install it first. You can do this by running the command below:

dotnet add package QueryFilters

After installing the package, you can import it into your project by adding the using statements

global using QueryFilters.Filters;
global using QueryFilters.Extensions;

Next, you can create your filter class by inheriting from the QueryFilter class. If you have installed the template package QueryFilterTemplate you can create a filter file from the template 'filter' dotnet new filter --entityType TestObject --name TestObjectFilter You can then add the properties you want to filter by as shown below:

public class TestObjectFilter : QueryFilter
{
    public string? Name { get; set; }
}

public static class TestObjectFilterExtensions
{
    //Filter method must have the same name as the property it filters.

    public static IQueryable<TestObject> Name(this IQueryable<TestObject> query, string value)
    {
        //Implement the filter logic here
        query = query.Where(x => x.Name == value);
        return query;
    }

}

The base class QueryFilter already has a few properties predefined for you.

public abstract class QueryFilter
{
    [Range(1, int.MaxValue)]
    public int? Page { get; set; }
    [Range(1, 1000)]
    public int? PerPage { get; set; }
    public string? SortField { get; set; }
    public string SortDirection { get; set; } = "asc";

}

Usage

To use our newly created filter, we can just call the Filter or FilterAndPaginate method on our data source and pass in the filter object.

var query = _context.TestObjects;
var filter = new TestObjectFilter { Name = "Test", Page = 1, PerPage = 10, SortField = "Name", SortDirection = "desc" };
result = query.ApplyFilter(filter);

In most cases, this filter object will be created from the query string in the request.

[HttpGet]
public IActionResult Get([FromQuery] TestObjectFilter filter)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    var query = _context.TestObjects;

    return Ok(query.FilterAndPaginate(filter));
}

The result

The Filter method will just return a List<T> of the filtered data, while the FilterAndPaginate method will return a PaginatedResponse<T>:

public class PaginatedResponse<T>
{
    public int CurrentPage { get; set; }
    public int PerPage { get; set; }
    public int Total { get; set; }
    public required List<T> Data { get; set; }
    public int LastPage => (int)Math.Ceiling((double)Total / PerPage);
}

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  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.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on QueryFilters:

Package Downloads
QueryFilterTemplate

Templates for easy query filtering in apis

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.3 450 7/24/2025
1.0.2 470 7/24/2025
1.0.1 126 9/30/2024