Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat does Expression.Quote() do that Expression.Constant() can’t already do?
    primarykey
    data
    text
    <p><strong>Note: I am aware of the earlier question “<a href="https://stackoverflow.com/questions/3138133/">What is the purpose of LINQ's Expression.Quote method?</a>”</strong>, but if you read on you will see that it doesn’t answer my question.</p> <p>I understand what the stated purpose of <code>Expression.Quote()</code> is. However, <code>Expression.Constant()</code> can be used for the same purpose (in addition to all the purposes that <code>Expression.Constant()</code> is already used for). Therefore, I don’t understand why <code>Expression.Quote()</code> is at all required. </p> <p>To demonstrate this, I have written a quick example where one would customarily use <code>Quote</code> (see the line marked with exclamation points), but I used <code>Constant</code> instead and it worked equally well:</p> <pre><code>string[] array = { "one", "two", "three" }; // This example constructs an expression tree equivalent to the lambda: // str =&gt; str.AsQueryable().Any(ch =&gt; ch == 'e') Expression&lt;Func&lt;char, bool&gt;&gt; innerLambda = ch =&gt; ch == 'e'; var str = Expression.Parameter(typeof(string), "str"); var expr = Expression.Lambda&lt;Func&lt;string, bool&gt;&gt;( Expression.Call(typeof(Queryable), "Any", new Type[] { typeof(char) }, Expression.Call(typeof(Queryable), "AsQueryable", new Type[] { typeof(char) }, str), // !!! Expression.Constant(innerLambda) // &lt;--- !!! ), str ); // Works like a charm (prints one and three) foreach (var str in array.AsQueryable().Where(expr)) Console.WriteLine(str); </code></pre> <p>The output of <code>expr.ToString()</code> is the same for both, too (whether I use <code>Constant</code> or <code>Quote</code>).</p> <p>Given the above observations, it appears that <code>Expression.Quote()</code> is redundant. The C# compiler could have been made to compile nested lambda expressions into an expression tree involving <code>Expression.Constant()</code> instead of <code>Expression.Quote()</code>, and any LINQ query provider that wants to process expression trees into some other query language (such as SQL) could look out for a <code>ConstantExpression</code> with type <code>Expression&lt;TDelegate&gt;</code> instead of a <code>UnaryExpression</code> with the special <code>Quote</code> node type, and everything else would be the same.</p> <p>What am I missing? Why was <code>Expression.Quote()</code> and the special <code>Quote</code> node type for <code>UnaryExpression</code> invented?</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.
 

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