Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So from the point of view of expression the value is a constant. It can not be changed by the expression. </p> <p>What you have is a potentially open closure - i.e. the value can change between executions of the expression, but not during it. So it is a "constant". This is a paradigm difference between the world of functional programming and un-functional :) programming. </p> <p>Consider</p> <pre><code> int a =2; Expression&lt;Func&lt;int, int&gt;&gt; h = x=&gt; x+ a; Expression&lt;Func&lt;int, int&gt;&gt; j = x =&gt; x +2; a = 1; </code></pre> <p>the term a is a member access into an anonymous class that wraps up and access the a variable on the stack. The first node is a MemberAccess node then underneath that - the expression is a constant. </p> <p>For the code above:</p> <pre><code>((SimpleBinaryExpression)(h.Body)).Right {value(WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0).a} CanReduce: false DebugView: ".Constant&lt;WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0&gt;(WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0).a" Expression: {value(WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0)} Member: {Int32 a} NodeType: MemberAccess Type: {Name = "Int32" FullName = "System.Int32"} </code></pre> <p>And the constant underneath that:</p> <pre><code>((MemberExpression)((SimpleBinaryExpression)(h.Body)).Right).Expression {value(WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0)} CanReduce: false DebugView: ".Constant&lt;WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0&gt;(WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0)" NodeType: Constant Type: {Name = "&lt;&gt;c__DisplayClass0" FullName = "WindowsFormsApplication6.Form1+&lt;&gt;c__DisplayClass0"} Value: {WindowsFormsApplication6.Form1.} } } </code></pre> <p>The plain old 2 comes out to a:</p> <pre><code>((SimpleBinaryExpression)(j.Body)).Right {2} CanReduce: false DebugView: "2" NodeType: Constant Type: {Name = "Int32" FullName = "System.Int32"} Value: 2 </code></pre> <p>So I don't know if that helps you or not. You can kind of tell by looking at the parent node - or the type of object being accessed by the parent node.</p> <hr> <p>Adding as a result of your clarification - </p> <p>so when you say </p> <pre><code>user =&gt; user.Email == email </code></pre> <p>You mean look for all users with an email equal to a passed in parameter - however that link expression means something quite different. </p> <p>what you want to say is </p> <pre><code>Expression&lt;Func&lt;User, string, bool&gt;&gt; (user, email) =&gt; user.Email == email </code></pre> <p>This way the email will now be a parameter. If you don't like that there is one other thing you can do. </p> <p>The second example will work just fine - no extra params are needed consts will be consts. </p> <pre><code>t =&gt; t.Status != TaskStatus.Done &amp;&amp; t.Status != TaskStatus.Failed </code></pre> <hr> <p>Edit: adding another way:</p> <p>So one of the things that you had to do to get your code working was declare a string email outside the lambda - that is kind of clunky. </p> <p>You could better identify parameters by conventionally putting them in a specific place - like a static class. Then when going through the Lambda you don't have to look at some horrible cloture object - but a nice static class of your making. </p> <pre><code>public static class Parameter { public static T Input&lt;T&gt;(string name) { return default(T); } } </code></pre> <p>Then your code looks like this:</p> <pre><code>Expression&lt;Func&lt;User, bool&gt;&gt; exp = x =&gt; x.Email == Parameter.Input&lt;String&gt;("email"); </code></pre> <p>You can then traverse the tree - when you come to a call to to the Parameter static class you can look at the type and the name (in the arguments collection) and off you go.... </p>
    singulars
    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