DEV Community

Phúc Nguyễn
Phúc Nguyễn

Posted on

A Flexible Way to Filter Your IQueryable in .NET

Filtering data dynamically in a clean and reusable way is often a common challenge in .NET applications—especially when working with complex APIs or building search interfaces.

This Nuget package will help you build powerful and flexible filtering logic using C# Expression Trees.

What is dotnet-dynamic-filter-expression?

Packge dotnet-dynamic-filter-expression allows you to dynamically build LINQ IQueryable filters based on runtime conditions, such as query parameters from APIs or user-generated filters from front-end applications.

The goal is to eliminate boilerplate filtering logic and make your code more declarative and maintainable.

Why Use It?

✔ Dynamic & Composable: Build filters at runtime using expression trees.

✔ Strongly Typed: Safe and type-checked filtering for your entity models.

✔ Minimal Setup: Add the NuGet package, define your conditions, and apply filters—it’s that simple.

✔ Clean API: Designed to feel natural to .NET developers.

Installation

dotnet add package dotnet-dynamic-filter-expression
Enter fullscreen mode Exit fullscreen mode

Example Usage

Suppose you have these Customer and EmailAddress classes

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public Datetime BirthDay { get; set; }
    public EmailAddress Email { get; set; }
}

public class EmailAddress 
{
    public string Address { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

You can build dynamic filter like this

using FilterExpression;
using FilterExpression.Extension;

var list = new List<Customer>
{
    new Customer{ Name = "Name 1", Age = 25, Id = 1, BirthDay = new DateTime(1997, 9, 15), Email = new EmailAddress {Address = "email1@gmail.com} },

    new Customer{ Name = "Name 2", Age = 35, Id = 2, BirthDay = new DateTime(1990, 1, 1), Email = new EmailAddress {Address = "email2@gmail.com} },

    new Customer{ Name = "Name 3", Age = 15, Id = 3, BirthDay = new DateTime(2000, 5, 1),Email = new EmailAddress {Address = "email3@gmail.com}  },
};

var filterService = new FilterService();
var filter = filterService.Filter<Customer>("(Name eq `Name 1`)");

var filteredList = list.Where(filter.Compile()).ToArray();

// Another way
var filteredList = list.Filter("((Name contains `Name`) & (Age lt `30`))");

// Supported for IQueryable
var filteredList = list.AsQueryable().Filter("(BirthDay le `1997-9-15`)").ToList();

// Supported for nested object
var filteredList = list.AsQueryable().Filter("(Email.Address eq `email2@gmail.com`)").ToList();

Enter fullscreen mode Exit fullscreen mode

Supported Operators

dotnet-dynamic-filter-expression supports a wide range of comparison operators out of the box, including:

- Equal: (Name eq `John Doe`)
- Not Equal: (Name ne `John Doe`)
- Greater than: (Age gt `25`)
- Greater than and equal (Age ge `25`)
- Less than: (Age lt `25`)
- Less than and equal (Age le `25`)
- Contains: (Name contains `P`)
- StartsWith: (Name StartsWith `P`)
- In: (Age in `[25, 26]`)
- And: ( (Age eq `25`) & (Name eq `John`))
- Or: ( (Age eq `25`) | (Name eq `John`))
- Not: !(Name eq `John Doe`)
Enter fullscreen mode Exit fullscreen mode

You can also extend it with your own custom operators.

This package ideal for

  • Building admin dashboards with flexible filtering
  • Creating search APIs with user-defined criteria
  • Eliminating repetitive Where(...) logic in services

Under the Hood

The library uses expression trees to build strongly-typed lambda expressions that are directly compatible with IQueryable. This ensures performance and compatibility with LINQ providers such as Entity Framework Core.

Feedback & Contributions

This is an open-source project and still evolving. I’d love to hear your feedback, issues, or ideas to make it better.

👉 GitHub Repository: https://github.com/phucthanh6554/dotnet-dynamic-filter-expression
📦 NuGet: https://www.nuget.org/packages/dotnet-dynamic-filter-expression

Top comments (0)