Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I compose an expression tree from the union of several expressions?
    text
    copied!<p>I'm trying to construct an <code>IQueryable</code> 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.</p> <p>Here's what I have so far:</p> <pre><code>public class FilterManager&lt;T&gt; { public List&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; Inclusive { get; set; } public List&lt;Expression&lt;Func&lt;T, bool&gt;&gt;&gt; Exclusive { get; set; } public IQueryable&lt;T&gt; ApplyFilters(IQueryable&lt;T&gt; query) { var q = query; Exclusive.ForEach(exp =&gt; q = q.Where(exp)); //works fine Inclusive.ForEach(exp =&gt; /* ??? */); return q; } //ctor, etc. } </code></pre> <p>The idea here is that I add several Expressions to <code>Inclusive</code>, which "Ors" them together. For example, if <code>T</code> is an <code>int</code>, the code:</p> <pre><code>fm.Inclusive.Add(x =&gt; x &gt; 1); fm.Inclusive.Add(y =&gt; y &lt; 5); query = fm.ApplyFilters(query); </code></pre> <p>should have the same result set as:</p> <pre><code>query = query.Where(z =&gt; z &gt; 1 || z &lt; 5); </code></pre> <p>How can I get <code>Inclusive</code> 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.</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload