amnats / qbt-net

Query by Text in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Query by Text in C#

In a yak shaving accident I implemented a stripped down version of the OData provider. One can even say that it was a good exercise in understanding C# expression trees and LINQ queries. All that this library can do is transform a text query into an expression tree consumable by a LINQ provider.

It can recognise field names and even discern strings from numbers!

Example

var expressionBuilder { get; } = new FilterExpressionBuilder<User>();

var filterStr = "userName eq 'Luke Skywalker'";
var expr = expressionBuilder.TranslateToExpression(filterStr);
var filtered = Users.Where(expr).ToList();

Assert.That(filtered.Count, Is.EqualTo(1));
Assert.That(filtered.First.UserName, Is.EqualTo("Luke Skywalker"));
public class User
{
  public string UserName { get; set; }
}

Supported binary operators:

  • eq Equal
  • ne Not equal
  • gt Greater than
  • lt Less than
  • ge Grater than or equal
  • le Less than or equal
  • and And
  • or Or

Nice things to have in future:

  • Support both for camel case and pascal case field names in the text query
  • Support for more than one operator at a time
  • Bracket support
  • Order by
  • First
  • Last
  • string methods e.g. contains, endswith etc.
  • Take
  • Offset
  • Escaping characters

About

Query by Text in C#

License:MIT License


Languages

Language:C# 100.0%