CoreKit.DataFilter.Linq
1.0.0
dotnet add package CoreKit.DataFilter.Linq --version 1.0.0
NuGet\Install-Package CoreKit.DataFilter.Linq -Version 1.0.0
<PackageReference Include="CoreKit.DataFilter.Linq" Version="1.0.0" />
<PackageVersion Include="CoreKit.DataFilter.Linq" Version="1.0.0" />
<PackageReference Include="CoreKit.DataFilter.Linq" />
paket add CoreKit.DataFilter.Linq --version 1.0.0
#r "nuget: CoreKit.DataFilter.Linq, 1.0.0"
#:package CoreKit.DataFilter.Linq@1.0.0
#addin nuget:?package=CoreKit.DataFilter.Linq&version=1.0.0
#tool nuget:?package=CoreKit.DataFilter.Linq&version=1.0.0
CoreKit.DataFilter.Linq
This library allows you to apply dynamic filtering (FilterRequest
) to IQueryable<T>
in LINQ/EF Core using expression trees. It supports:
- Filtering with dynamic conditions
- Sorting with Dynamic LINQ
- Pagination using Skip/Take
๐ง Key Component
GenericLinqFilterProcessor<T>
Builds a Func<T, bool>
expression tree based on a FilterRequest
and applies it to a LINQ query.
โ Usage
With ApplyFilter(...)
var processor = new GenericLinqFilterProcessor<Product>();
var query = processor.ApplyFilter(_dbContext.Products, request);
With extension method:
var result = _dbContext.Products
.AsQueryable()
.ApplyFilterRequest(request)
.ToList();
Requires reference to
CoreKit.DataFilter.Linq.Extensions
๐ฆ Supported Features
- All
FilterOperatorEnum
operators - Nested groups with
AND
/OR
- IN, NULL, NOT NULL support
- Sorting via System.Linq.Dynamic.Core
๐ฆ NuGet Requirements
dotnet add package System.Linq.Dynamic.Core
๐ฏ How to receive filters in your Controller
You can support both GET
and POST
methods in your controller for filtering, but they have different behaviors:
โ Option 1: POST (Recommended for complex filters)
[HttpPost("search")]
public IActionResult Search([FromBody] FilterRequest request)
{
var result = _dbContext.Products
.AsQueryable()
.ApplyFilterRequest(request)
.ToList();
return Ok(result);
}
โ ๏ธ Option 2: GET (Limited support)
[HttpGet("search")]
public IActionResult Search([FromQuery] SimpleQueryRequest query)
{
var request = query.ToFilterRequest();
var result = _dbContext.Products
.AsQueryable()
.ApplyFilterRequest(request)
.ToList();
return Ok(result);
}
โ ๏ธ Limitations of GET filtering
Limitation | Description |
---|---|
No body | GET requests cannot use [FromBody] , so the entire filter must fit in the query string |
No nested groups | Only flat FilterRule lists are supported (no AND/OR groups) |
Limited operators | Only basic eq , neq , gt , lt , in , null , etc. |
Limited characters | Query strings are size-limited and must be URL-encoded |
๐งฎ How to use SimpleQueryRequest in a GET
๐ Query string parameters
Parameter | Description |
---|---|
filterQuery |
A semicolon-separated list of simple filters |
sort |
Field name to sort by |
sortDir |
Sorting direction: asc or desc |
page |
Page number (starting from 1) |
pageSize |
Number of items per page |
๐งช Example GET request
GET /api/products/search
?filterQuery=status:eq:active;price:gt:100
&sort=createdAt
&sortDir=desc
&page=2
&pageSize=10
๐งพ Syntax for filterQuery
Each filter is composed of:
field:operator:value
- Use
;
to separate multiple rules. - To use OR logic, prefix each rule with
or:
.
โ
Supported operators in filterQuery
Operator | Syntax | Example |
---|---|---|
Equals | eq |
status:eq:active |
Not Equals | neq |
status:neq:disabled |
Greater Than | gt |
price:gt:100 |
Less Than | lt |
price:lt:500 |
Greater Than or Equal | gte |
stock:gte:10 |
Less Than or Equal | lte |
stock:lte:5 |
Contains | contains |
name:contains:book |
Starts With | starts |
name:starts:Pro |
Ends With | ends |
name:ends:X |
In List | in |
category:in:Books,Electronics |
Is Null | null |
deletedAt:null |
Is Not Null | notnull |
deletedAt:notnull |
๐ง Important Notes
Values in
in
must be comma-separated and not quoted.Use
or:
prefix for logical OR:or:category:eq:Books;or:category:eq:Stationery
Nested groups and complex logical combinations (
AND
+OR
) are not supported in GET.
๐ง Controller example
[HttpGet("search")]
public IActionResult Search([FromQuery] SimpleQueryRequest query)
{
var request = query.ToFilterRequest();
var result = _dbContext.Products
.AsQueryable()
.ApplyFilterRequest(request)
.ToList();
return Ok(result);
}
๐ฌ How to use FilterRequest in a POST
Use POST when you need full filtering capabilities including:
- Grouped filters (
AND
/OR
) - Operators like
IN
,NULL
,NOT NULL
- Large payloads not suitable for query strings
๐ฆ Example JSON body
{
"filter": {
"logic": "and",
"rules": [
{ "field": "status", "operator": "Equals", "value": "active" },
{ "field": "price", "operator": "GreaterThan", "value": "100" }
],
"groups": [
{
"logic": "or",
"rules": [
{ "field": "category", "operator": "Equals", "value": "Books" },
{ "field": "category", "operator": "Equals", "value": "Stationery" }
]
}
]
},
"sort": [
{ "field": "createdAt", "direction": "desc" }
],
"pagination": {
"page": 1,
"pageSize": 20
}
}
๐ง Controller example
[HttpPost("search")]
public IActionResult Search([FromBody] FilterRequest request)
{
var result = _dbContext.Products
.AsQueryable()
.ApplyFilterRequest(request)
.ToList();
return Ok(result);
}
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
- CoreKit.DataFilter (>= 1.0.0)
- System.Linq.Dynamic.Core (>= 1.6.3)
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.0 | 108 | 5/23/2025 |