Powerful filter builder that converts string-based queries to expressions Expression<Func<T,bool>>
that you can execute against your code, collections, Entity Framework, and so much more.
- You must have Visual Studio 2019 Community or higher.
- The dotnet cli is also highly recommended.
This library was created to convert text commands to expressions in order to apply queries generated by users in front-end applications to backend services such as the data access layers.
- Run
dotnet restore
- Run
dotnet build
- Run
dotnet test
Use the NuGet package manager to install Dime.Expressions
:
- dotnet cli:
dotnet add package Dime.Expressions
- Package manager:
Install-Package Dime.Expressions
using System.Linq.Expressions;
public class Customer
{
public bool IsActive { get; set; }
}
public class CustomerFilter
{
public static Expression<Func<Customer, bool>> CreateFilter(string property, string operation, string val)
{
ExpressionBuilder builder = new ExpressionBuilder();
builder.WithDateTimeParser(new DateTimeParser("Europe/London", new CultureInfo("en-GB")));
builder.WithDoubleParser(new DoubleParser("en-GB"));
builder.WithDecimalParser(new DecimalParser("en-GB"));
return ((IFilterExpressionBuilder)builder).GetExpression<Customer>(property, operation, val);
}
}
public class CustomerApiController : ControllerBase
{
/// <summary>
/// Example data:
/// property = "IsActive"
/// operatorKey = "eq"
/// value = "true"
/// </summary>
public async Task<IEnumerable<Customer>> Get(string property, string operatorKey, string value)
{
var filter = CustomerFilter.CreateFilter(property, operatorKey, value); // x => x.IsActive == true;
return await dbContext.Customers.Where(filter).ToListAsync();
}
}
We welcome contributions. Please check out the contribution and code of conduct guidelines first.
To contribute:
- Fork the project
- Create a feature branch (
git checkout -b feature/mynewfeature
) - Commit your changes (
git commit -m 'Add mynewfeature'
) - Push to the branch (
git push origin feature/mynewfeature
) - Open a pull request