QueryFilters 1.0.3
dotnet add package QueryFilters --version 1.0.3
NuGet\Install-Package QueryFilters -Version 1.0.3
<PackageReference Include="QueryFilters" Version="1.0.3" />
<PackageVersion Include="QueryFilters" Version="1.0.3" />
<PackageReference Include="QueryFilters" />
paket add QueryFilters --version 1.0.3
#r "nuget: QueryFilters, 1.0.3"
#:package QueryFilters@1.0.3
#addin nuget:?package=QueryFilters&version=1.0.3
#tool nuget:?package=QueryFilters&version=1.0.3
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 | Versions 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. |
-
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.