Skip to content

🖇️ A powerful library that converts string-based filters to C# expressions.

License

Notifications You must be signed in to change notification settings

dimesoftware/expressions

Repository files navigation

.NET Expressions Builder

Introduction

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.

Getting Started

  • You must have Visual Studio 2019 Community or higher.
  • The dotnet cli is also highly recommended.

About this project

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.

Build and test

  • Run dotnet restore
  • Run dotnet build
  • Run dotnet test

Installation

Use the NuGet package manager to install Dime.Expressions:

  • dotnet cli: dotnet add package Dime.Expressions
  • Package manager: Install-Package Dime.Expressions

Usage

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();
  }
}

Contributing

We welcome contributions. Please check out the contribution and code of conduct guidelines first.

To contribute:

  1. Fork the project
  2. Create a feature branch (git checkout -b feature/mynewfeature)
  3. Commit your changes (git commit -m 'Add mynewfeature')
  4. Push to the branch (git push origin feature/mynewfeature)
  5. Open a pull request

About

🖇️ A powerful library that converts string-based filters to C# expressions.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages