https://github.com/devkennies26/dynamic-filter-component
DynamicFilter is a lightweight and extensible .NET library for dynamically filtering and sorting entities in an Entity Framework Core DbContext based on string-based filter expressions.
https://github.com/devkennies26/dynamic-filter-component
custom-filtering-system dynamic-filtering dynamic-search-filtering generic-filtering generic-search-filtering
Last synced: 5 months ago
JSON representation
DynamicFilter is a lightweight and extensible .NET library for dynamically filtering and sorting entities in an Entity Framework Core DbContext based on string-based filter expressions.
- Host: GitHub
- URL: https://github.com/devkennies26/dynamic-filter-component
- Owner: devKennies26
- Created: 2025-07-18T07:28:18.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-18T10:19:22.000Z (11 months ago)
- Last Synced: 2025-09-24T10:50:44.361Z (8 months ago)
- Topics: custom-filtering-system, dynamic-filtering, dynamic-search-filtering, generic-filtering, generic-search-filtering
- Language: C#
- Homepage: https://www.nuget.org/packages/DynamicFilter.Component
- Size: 15.6 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# DynamicFilter.Component
DynamicFilter is a lightweight and extensible .NET library for dynamically filtering and sorting entities in an Entity Framework Core DbContext based on string-based filter expressions. It's ideal for building flexible APIs where filter logic needs to be dynamic and runtime-driven.
## Features
- Filter any EF Core entity by name and a simple filter string
- Supports nested property sorting (e.g., `sortBy=Author.Name`)
- Automatically resolves entities via `DbContext` metadata
- Easily extensible for additional filter logic
- Minimal setup via `IServiceCollection` extension
## Usage
Install via NuGet Package Manager:
```bash
dotnet add package DynamicFilter.Component
```
1. Register the Service
Add the following in your `Program.cs` or wherever you configure services:
```csharp
builder.Services.AddDynamicFilter();
```
2. Inject and Use the Filter Service
```csharp
public class MyController : ControllerBase
{
private readonly IDynamicFilterService _filterService;
public MyController(IDynamicFilterService filterService)
{
_filterService = filterService;
}
[HttpGet("filter")]
public async Task Filter(string filter, string entityName)
{
var results = await _filterService.FilterEntitiesAsync(filter, entityName);
return Ok(results);
}
}
```
## Filter Syntax
The filter string is a comma-separated list of key-value pairs. Supported keys:
- `[propertyName]=[value]` — Basic filtering by property
- `sortby=[PropertyName]` — Sort by the specified property (supports nested properties)
### Example
```plaintext
Example 1: name=John,age=30,sortby=CreatedDate,sortdescending=true
Example 2: category=Electronics, isinstock=true, sortby=price for Product entity
Example 3: category=Electronics, isinstock=true, sortby=price, maxprice=550 for Product
Example 4: sortby=price, maxprice=550, minprice=222 for Product
Example 5: sortby=price, maxprice=550, minprice=222, price=333 (hard equality is ignoring) for Product
Example 6: sortby=price, maxprice=550, price=220 (hard equality is ignoring) for Product
Example 7: Roles.Name=Admin, sortby=CreatedDate
```