Skip to content
tghamm edited this page Mar 27, 2019 · 5 revisions

A couple of simple examples:

First: pull down the code and run the sample MVC project to see a working implementation.

Getting Started:

At it's most basic, dynamic-linq-query-builder accepts a class to be filtered, and a filter object that replicates the structure and properties of the jQuery QueryBuilder filter object. Additionally, there is a helper class to produce a default description of the class to jQuery Querybuilder. It will get you started, though you may wish to further customize it to control the builder's behavior and capabilities.

There are two primary ways to use the library. In default mode, where every execution builds the expression tree, or in predicate mode, where the expression tree is assigned to a variable and can be repeatedly used for increased performance.

Test Data:

public class Person
{
   public string FirstName {get; set;}
   public string LastName {get; set;}
}

var people = new List<People>() { 
                     new Person() { FirstName = "John", LastName = "Doe" }, 
                     new Person() { FirstName = "Jane", LastName = "Doe" }};

var myFilter = new QueryBuilderFilterRule()
            {
                Condition = "and",
                Rules = new List<QueryBuilderFilterRule>()
                {
                    new QueryBuilderFilterRule()
                    {
                        Condition = "and",
                        Field = "FirstName",
                        Id = "FirstName",
                        Input = "NA",
                        Operator = "equal",
                        Type = "string",
                        Value = new [] { "jane" }
                    }
                }
            };

//Indexed class example
private class IndexedClass
        {
            int this[string someIndex]
            {
                get
                {
                    return 2;
                }
            }
        }
//Indexed filter
var indexFilter = new QueryBuilderFilterRule
            {
                Condition = "and",
                Field = "ContentTypeId",
                Id = "ContentTypeId",
                Input = "NA",
                Operator = "equal",
                Type = "integer",
                Value = new [] { "2" }
            };

Usage Example 1:

//Compile the expression and return the results
var result = people.BuildQuery<Person>(myFilter).ToList();
//results in 1 record returned

Usage Example 2:

//Note this example, while trivial, shows how the predicate can be re-used. 
//When appropriate, the performance gain of re-using the 
//predicate vs rebuilding is substantial.

var predicate = myFilter.BuildPredicate<Person>(
                       new BuildExpressionOptions() { ParseDatesAsUtc = true });
for (var x = 0; x < 100; x++)
{
   var result = people.Where(predicate).ToList();
}

Usage Example 3:

//Indexed class example
var result = new List<IndexedClass> {new IndexedClass()}.AsQueryable()
              .BuildQuery(indexFilter, 
              new BuildExpressionOptions() {
                                   UseIndexedProperty = true, 
                                   IndexedPropertyName = "Item"});

Advanced Options:

You can additionally specify options to the compiler through overloads that dictate some of the behavior. they are listed below:

//See BuildExpressionOptions class for all properties
CultureInfo - defaults to CultureInfo.InvariantCulture for numeric and date conversions.
ParseDatesAsUtc - treat dates as UTC when converting from string
UseIndexedProperty - allows the use of an indexed property
IndexedPropertyName - Property name of indexed property