Note that there are some explanatory texts on larger screens.

plurals
  1. POmanually build linq expression for x => x.Child == itemToCompare.Child
    text
    copied!<p>We have an object and we want to build a linq query based on that object on the fly. This linq statement is equivalent to what we want to build:</p> <pre><code>Expression&lt;Func&lt;Sample, bool&gt;&gt; linqExpression = x =&gt; x.Child == itemToCompare.Child; </code></pre> <p>We can't quite come up with the right expression to build the itemToCompare.Child part. Here's what we have so far:</p> <pre><code>var param = Expression.Parameter(typeof(T), "x"); var key = itemToCompare.GetType().GetProperty("Child"); var rhsConstant = Expression.Constant(item); var innerLambda = Expression.Lambda&lt;Func&lt;T&gt;&gt;(rhsConstant, new ParameterExpression[0]); var rhsMemberAccess = Expression.MakeMemberAccess(innerLambda, key); body = Expression.Equal(lhsPropertyAccess, rhsMemberAccess); var lambda = Expression.Lambda&lt;Func&lt;T, bool&gt;&gt;(body, param); </code></pre> <p>The expression tree for our hand-built query looks like:</p> <pre><code>Lambda Expression: x =&gt; (x.Child = value(SampleTests+Sample)) Expression Body: (x.Child = value(SampleTests+Sample)) Parameter 0: 'x', Type: Sample NodeType: Lambda Left Lambda Expression: x.Child Left NodeType: MemberAccess Lambda Expression: x Expression Member: Sample Child NodeType: Parameter -- a parameter expression Right Lambda Expression: value(SampleTests+Sample) Right NodeType: Constant NodeType: Constant Value: SampleTests+Sample </code></pre> <p>The expression tree for the actual lambda looks like:</p> <pre><code>Lambda Expression: x =&gt; (x.Child = value(SampleTests+&lt;&gt;c__DisplayClass15).itemToCompare.Child) Expression Body: (x.Child = value(SampleTests+&lt;&gt;c__DisplayClass15).itemToCompare.Child) Parameter 0: 'x', Type: Sample NodeType: Lambda Left Lambda Expression: x.Child Left NodeType: MemberAccess Lambda Expression: x Expression Member: Sample Child NodeType: Parameter -- a parameter expression Right Lambda Expression: value(SampleTests+&lt;&gt;c__DisplayClass15).itemToCompare.Child Right NodeType: MemberAccess Lambda Expression: value(SampleTests+&lt;&gt;c__DisplayClass15).itemToCompare Expression Member: Sample Child NodeType: MemberAccess Lambda Expression: value(SampleTests+&lt;&gt;c__DisplayClass15) Expression Member: Sample itemToCompare NodeType: Constant NodeType: Constant Value: SampleTests+&lt;&gt;c__DisplayClass15 </code></pre> <p>Edit:</p> <p>We think this works out to</p> <pre><code>Expression&lt;Func&lt;Sample, bool&gt;&gt; linqExpression = x =&gt; x.Child == (()=&gt;itemToCompare).Child; </code></pre> <p>which is what we're trying to reproduce in our expression. </p> <p>Ultimately the goal is to produce an Expression where the Child values are compared using their Type's .Equals() method.</p> <p>Thanks!</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