Overview of AutoFilterer
developIQueryable without manually writing LINQ expressions. Unlike oData or GraphQL, AutoFilterer's parameters and usage are designed to be compatible with the OpenAPI 3.0 Specification.repository·develop·Indexed 19 days ago
https://github.com/enisn/autofiltererA .NET library that automates the creation of LINQ expressions for filtering entities using DTOs. Designed as a lightweight, OpenAPI 3.0 compatible alternative to oData or GraphQL, it allows developers to generate complex queries for IQueryable without manual LINQ writing. Includes extensions such as AutoFilterer.Swagger for interactive API documentation, AutoFilterer.Generators for source-generated filter objects, and AutoFilterer.Dynamics for untyped filtering using dictionaries and JSON.
IQueryable without manually writing LINQ expressions. Unlike oData or GraphQL, AutoFilterer's parameters and usage are designed to be compatible with the OpenAPI 3.0 Specification.AutoFilterer.Generators package uses .NET source generators to automatically create filter objects from your entities. This reduces manual boilerplate when defining filter criteria for your domain models.To support advanced query logic, the library introduces two specific types:
OperatorFilter<T>: Used for struct types to handle various comparison operators.StringFilter: A specialized type designed specifically for string-based filtering operations.AutoFilterer.Generators uses .NET source generators to automatically create filter objects from your entities, reducing the boilerplate required to define filtering logic.By default, multiple filtering parameters are combined using the AND (&&) operator. You can change this behavior to use the OR (||) operator by setting the CombineWith property on your filter DTO.
If you are using Swagger, CombineWith appears as an enum parameter:
0 (or CombineType.And): Combines parameters with && (Default).1 (or CombineType.Or): Combines parameters with ||.This allows you to switch between strict filtering (all conditions must be met) and broad filtering (any condition can be met) dynamically.
// Default behavior (AND):
// Query: /Blogs?priority.min=4&isPublished=false
// Result: x => x.Priority > 4 && x.IsPublished == false
// OR behavior:
// Query: /Blogs?priority.min=4&isPublished=false&combineWith=1
// Result: x => x.Priority > 4 || x.IsPublished == falseYou can define custom complex types (like a Range<T> object) that have their own specific expression generation logic. To do this, create a class that implements the IFilterableType interface and provide your own implementation of the BuildExpression() method. AutoFilterer will automatically use this method when it encounters the type in a filter DTO.
// Example pattern for a custom type
public class MyCustomType : IFilterableType
{
// Implement BuildExpression to define how this type translates to a LINQ expression
public Expression BuildExpression(Expression expressionBody, PropertyInfo property, object value)
{
// Custom logic here
}
}The OperatorFilter<T> is used to perform dynamic comparisons on types that implement the IComparable interface (such as numeric fields). It implements IFilterableType, allowing it to be used as a property type within a FilterBase object. Unlike static filters, both the comparison operator and the comparison value are provided by the client via the query string.
public class BookFilter : FilterBase
{
public OperatorFilter<int> TotalPage { get; set; }
}It is important to understand the difference in how queries are generated for string properties:
x => x.Title.Equals("something", StringComparison.InvariantCultureIgnoreCase). This may fail on providers like MongoDB.x => x.Title == "something". This is the recommended approach for database providers that do not support the .Equals() method translation.AutoFilterer.Dynamics extension allows you to use the framework without predefined types. It achieves this by using dictionaries and plain JSON instead of class-based DTOs.Range<T> object) that have their own specific expression generation logic. To do this, create a class that implements the IFilterableType interface and provide your own implementation for the BuildExpression() method. AutoFilterer will automatically use this method when it encounters the type in a filtering DTO.AutoFilterer.Swagger extension improves Swagger documentation by automatically converting supported text fields into dropdown menus in the UI, making the API more interactive and user-friendly.AutoFilterer is a filtering framework for .NET designed to automatically generate LINQ expressions for Entities based on DTO (Data Transfer Object) filter models.
Key Concepts:
IQueryable (e.g., an Entity Framework DbSet).