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

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 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. 
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.0 108 5/23/2025