GreenNide.FilterGenerator 0.0.1-alpha.9

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

GreenNide.FilterGenerator

NuGet Version NuGet Downloads

A Roslyn source generator that auto-generates EF Core filter classes from expression tree definitions. You describe filters with static expression properties — the generator creates a standalone POCO class with an Apply() extension method that builds IQueryable<T>.Where(...).

Install

dotnet add package GreenNide.FilterGenerator --prerelease

Or in .csproj:

<PackageReference Include="GreenNide.FilterGenerator" Version="0.0.1-alpha.5" />

Quick Start

1. Define a filter

using GreenNide.ExpressionFilter;

[GenerateFilter(typeof(Order))]
public partial class OrderFilterDefinition
{
    // Equal (default for Guid?)
    public static Expression<Func<Order, Guid?>>? CustomerId { get; } = o => o.CustomerId;

    // Contains (default for string)
    public static Expression<Func<Order, string>>? Description { get; } = o => o.Description;

    // GreaterThanOrEqual
    [Compare(CompareOperator.GreaterThanOrEqual)]
    public static Expression<Func<Order, decimal?>>? MinAmount { get; } = o => o.Amount;

    // LessThanOrEqual
    [Compare(CompareOperator.LessThanOrEqual)]
    public static Expression<Func<Order, decimal?>>? MaxAmount { get; } = o => o.Amount;

    // Navigation with automatic null-guard
    public static Expression<Func<Order, string>>? CustomerName { get; } = o => o.Customer.Name;

    // Multi-column search
    public static Expression<Func<Order, string[]>>? Search { get; } =
        o => new[] { o.Description, o.Customer.Name, o.Customer.Email };
}

2. Use it

The generator produces a standalone OrderFilterParams POCO class (no inheritance from the definition):

var filter = new OrderFilterParams
{
    CustomerId = someId,
    MinAmount = 100m,
    Description = "Premium"
};

var results = await dbContext.Orders
    .Apply(filter)
    .ToListAsync();

3. Bind from HTTP requests

Since OrderFilterParams is a plain POCO, it works directly with model binding. Use [FromQuery] for GET or [FromBody] for POST:

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    private readonly AppDbContext _db;
    public OrdersController(AppDbContext db) => _db = db;

    // GET /api/orders?customerId=...&minAmount=100&description=premium
    [HttpGet]
    public IActionResult Get([FromQuery] OrderFilterParams filter)
    {
        var results = _db.Orders.Apply(filter).ToList();
        return Ok(results);
    }

    // POST /api/orders/filter  { "minAmount": 100, "description": "premium" }
    [HttpPost("filter")]
    public IActionResult Filter([FromBody] OrderFilterParams filter)
    {
        var results = _db.Orders.Apply(filter).ToList();
        return Ok(results);
    }
}

A working example project is in Examples/.

Supported Operators

Operator Description
Equal ==
NotEqual !=
GreaterThan >
GreaterThanOrEqual >=
LessThan <
LessThanOrEqual <=
Contains .Contains()
StartsWith .StartsWith()
EndsWith .EndsWith()

Auto-detection: string → Contains, everything else → Equal. Override with [Compare(...)].

Predicate Methods

Static methods with signature Expression<Func<TEntity, bool>>? Method(DefinitionType filter) let you write complex predicates:

[GenerateFilter(typeof(Order))]
public partial class OrderFilterDefinition
{
    public Guid? ItemId { get; set; }
    public int? MinItemCount { get; set; }

    public static Expression<Func<Order, bool>>? HasItem(OrderFilterDefinition filter) =>
        filter.ItemId.HasValue
            ? o => o.OrderItems.Any(i => i.Id == filter.ItemId.Value)
            : null;

    public static Expression<Func<Order, bool>>? HasMinItemCount(OrderFilterDefinition filter) =>
        filter.MinItemCount.HasValue
            ? o => o.OrderItems.Count >= filter.MinItemCount.Value
            : null;
}

Closure properties are automatically copied to the generated POCO class. This also works for collection types — e.g., filtering by multiple IDs:

[GenerateFilter(typeof(Product))]
public partial class ProductFilterDefinition
{
    // Closure property: a list of category IDs
    public List<Guid>? CategoryIds { get; set; }

    // Predicate: products whose CategoryId is in the list
    public static Expression<Func<Product, bool>>? InCategories(ProductFilterDefinition filter) =>
        filter.CategoryIds is { Count: > 0 }
            ? o => filter.CategoryIds.Contains(o.CategoryId!.Value)
            : null;
}

// Usage — the list binds from JSON body automatically:
// POST /api/products/filter  { "categoryIds": ["guid1", "guid2"] }
var filter = new ProductFilterParams
{
    CategoryIds = new List<Guid> { electronicsId, booksId }
};
var results = _db.Products.Apply(filter).ToList();

Custom Class Name

By default the generated class is named {EntityName}FilterParams. Override it with ClassName:

[GenerateFilter(typeof(Category), ClassName = "CategorySearchParams")]
public partial class CategoryFilterDefinition
{
    public static Expression<Func<Category, string>>? Name { get; } = o => o.Name;

    public static Expression<Func<Category, string[]>>? Search { get; } =
        o => new[] { o.Name };
}

// Generated class: CategorySearchParams (not CategoryFilterParams)
var filter = new CategorySearchParams { Name = "Electronics" };
var results = _db.Categories.Apply(filter).ToList();

Tests

dotnet test
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

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
0.0.1-alpha.9 95 7/29/2026
0.0.1-alpha.8 85 7/28/2026
0.0.1-alpha.7 85 7/28/2026
0.0.1-alpha.6 79 7/28/2026
0.0.1-alpha.5 88 7/26/2026

Initial prerelease. Source generator for type-safe collection filtering.