Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're attempting to build an expression tree that represents this:</p> <pre><code>c =&gt; true &amp;&amp; c.ClientFName == searchForClientFName </code></pre> <p>You are actually building an expression tree that represents this:</p> <pre><code>c =&gt; c=&gt; true &amp;&amp; c =&gt; c.ClientFName == searchForClientFName </code></pre> <p>which makes no sense at all.</p> <p>Now, you might naively think that this will work:</p> <pre><code>public static Expression&lt;TDelegate&gt; AndAlso&lt;TDelegate&gt;(this Expression&lt;TDelegate&gt; left, Expression&lt;TDelegate&gt; right) { // NOTICE: Combining BODIES: return Expression.Lambda&lt;TDelegate&gt;(Expression.AndAlso(left.Body, right.Body), left.Parameters); } </code></pre> <p>That would produce in your case something representing</p> <pre><code>c =&gt; true &amp;&amp; c.ClientFName == searchForClientFName </code></pre> <p>Which looks right. But in fact this is fragile. Suppose you had</p> <pre><code>... d =&gt; d.City == "London" ... ... c =&gt; c.ClientName == "Fred Smith" ... </code></pre> <p>and you used this method to combine them. You'd get an object representing</p> <pre><code>c =&gt; d.City == "London" &amp;&amp; c.ClientName == "Fred Smith" </code></pre> <p>What the heck is d doing in there?</p> <p>Furthermore, parameters are matched <em>by object identity</em>, not <em>by parameter name</em>. If you do this</p> <pre><code>... c =&gt; c.City == "London" ... ... c =&gt; c.ClientName == "Fred Smith" ... </code></pre> <p>and combine them into</p> <pre><code>c =&gt; c.City == "London" &amp;&amp; c.ClientName == "Fred Smith" </code></pre> <p>you're in the same boat; the "c" in "c.City" is a <em>different c</em> than the other two.</p> <p>What you actually need to do is make a <em>third</em> parameter object, <em>substitute</em> it in the bodies of both lambdas for every occurence of their parameters, and then build up a <em>new lambda expression tree</em> from the resulting substituted bodies.</p> <p>You can build a substitution engine by writing a visitor that passes over the expression tree body, rewriting it as it goes.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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