This is a simple parser for LHS Bracket syntax.
Install LhsBracketParser Package by running this command in Package Manager Console:
Install-Package LhsBracketParser -Version 1.0.3
We needed some advanced filtering in our ASP.NET WebApi based project. I couldn't find any good library to
turn predicates like (CreateDate[lt]2018-12-20 or Creator[eq]"Jalal") and IsNew[eq]true to an ORM based
predicate.
I have already implemented an Evaluator for LLBLGen PredicateExpression type.
You can implement your own by inheriting from EvaluatorBase class.
In your application, get filter from query string and apply it to your repository like sample below. I am using LLBLGen as ORM so I instantiate PredicateExpressionEvaluator but you can implement your own by extending EvaluatorBase for EntityFramework or NHibernate.
// https://mysite.com/products?filter=(CreateDate[lt]2018-12-20 or Creator[eq]"Jalal") and IsNew[eq]true
[HttpGet]
public IActionResult Get(string filter)
{
var evaluator = new PredicateExpressionEvaluator();
var predicate = (PredicateExpression)evaluator.Evaluate(filter);
var entities = repository.GetCollectionBy(predicate);
/// ...
}
| Operator | Bracket Notation |
|---|---|
| N/A | [range] |
| N/A | [like] |
| = | [eq] |
| != | [ne] |
| > | [gt] |
| >= | [gte] |
| < | [lt] |
| <= | [lte] |
| and | and |
| or | or |