I'm trying to construct an IQueryable
which will be evaluated by my entity model. I want to pass it two groups of lambdas and have it compose everything into a more complex expression tree, which is passed on to the database for execution.
Here's what I have so far:
public class FilterManager<T>
{
public List<Expression<Func<T, bool>>> Inclusive { get; set; }
public List<Expression<Func<T, bool>>> Exclusive { get; set; }
public IQueryable<T> ApplyFilters(IQueryable<T> query)
{
var q = query;
Exclusive.ForEach(exp => q = q.Where(exp)); //works fine
Inclusive.ForEach(exp => /* ??? */);
return q;
}
//ctor, etc.
}
The idea here is that I add several Expressions to Inclusive
, which "Ors" them together. For example, if T
is an int
, the code:
fm.Inclusive.Add(x => x > 1);
fm.Inclusive.Add(y => y < 5);
query = fm.ApplyFilters(query);
should have the same result set as:
query = query.Where(z => z > 1 || z < 5);
How can I get Inclusive
to work without third-party tools such as PredicateBuilder? Third-party tools are usually fine, but I'd like to improve my understanding of how to compose expressions in .NET.
I also need to make sure that the tree won't be evaluated yet, so that I can do the filtering on the database. That means I'll need to produce something Entity Framework 4.0 can consume.