Everybody loves the LinqKit library by Joe Albahari, and its PredicateBuilder.
However, it has a drawback due to the usage of Expression<> : Expression needs to be compiled, which takes a lot of time when you use it oftenly. In some of my treatments, I noticed using a profiler than more of 50% of the processing time of my operations seems to be lost in expression's compilation.
For Linq to SQL, the compilation phase of the expression is mandatory and I perfectly understand why LinqKit use it, but for Linq to Objects, working from start to end with a simple Func should perfectly work.
But obviously, if you do so you will lose the benefit of the nice syntax of PredicateBuilder and its combination operators.
Giving for example:
public Func<Thing, bool> WherePredicate()
{
var func = new Func<Thing, bool>(f => f.MyTest);
if (something)
{
func = f => f.MyOtherTest && func(f);
}
return func;
}
Instead of :
public Func<Thing, bool> WherePredicate()
{
var func = PredicateBuilder.New<Thing>(true);
func = func.And(f => f.MyTest);
if (something)
{
func = func.And(f => f.MyOtherTest);
}
return func;
}
Of course real world examples are much more complicated of the simple predicate above.
I would like to know if there is one easy solution that allows to combine advantages of each approach, for example using another library than LinqKit that works with Func<> instead of Expression> but with the same syntax.