Skip to content

Compile and Reuse

You can access Gridify generated expressions using the GetFilteringExpression of GridifyQuery or BuildCompiled methods of QueryBuilder class, by storing an expression you can use it multiple times without having any overheads, also if you store a compiled expression you get a massive performance boost.

WARNING

You should only use a compiled expression (delegate) if you are not using Gridify alongside an ORM like Entity-Framework.

csharp
// eg.1 - using GridifyQuery - Compiled - where only
var gq = new GridifyQuery() { Filter = "name=John" };
var expression = gq.GetFilteringExpression<Person>();
var compiledExpression = expression.Compile();
var result = persons.Where(compiledExpression);
csharp
// eg.2 - using QueryBuilder - Compiled - where only
var compiledExpression = new QueryBuilder<Person>()
                         .AddCondition("name=John")
                         .BuildFilteringExpression()
                         .Compile();
var result = persons.Where(compiledExpression);
csharp
// eg.3 - using QueryBuilder - BuildCompiled
var func = new QueryBuilder<Person>()
          .AddCondition("name=John")
          .BuildCompiled();
var result = func(persons);

Performance

This is the performance improvement example when you use a compiled expression.

MethodMeanRatioRatioSDGen 0Gen 1Allocated
GridifyCompiled1.008 us0.0010.000.1564-984 B
Gridify689.329 us1.0000.005.85942.929739,924 B
NativeLINQ736.854 us1.0190.015.85942.929737,392 B