title: How to convert an expression tree to a partial SQL query?
body: <p>When EF or LINQ to SQL runs a query, it:</p>
  
  <ol>
  <li>Builds an expression tree from the code,</li>
  <li>Converts the expression tree into an SQL query,</li>
  <li>Executes the query, gets the raw results from the database and converts them to the result to be used by the application.</li>
  </ol>
  
  <p>Looking at the stack trace, I can't figure out where the second part happens.</p>
  
  <p>In general, is it possible to use an existent part of EF or (preferably) LINQ to SQL to convert an <code>Expression</code> object to a partial SQL query (using Transact-SQL syntax), or I have to reinvent the wheel?</p>
  
  <hr>
  
  <p><strong>Update:</strong> a comment asks to provide an example of what I'm trying to do.</p>
  
  <p>Actually, <a href="https://stackoverflow.com/questions/7731905/how-to-convert-an-expression-tree-to-a-partial-sql-query/7891426#7891426">the answer by Ryan Wright below</a> illustrates perfectly what I want to achieve as a result, except the fact that my question is specifically about <em>how can I do it by using existent mechanisms of .NET Framework actually used by EF and LINQ to SQL</em>, instead of having to reinvent the wheel and write thousands of lines of not-so-tested code myself to do the similar thing.</p>
  
  <p>Here is also an example. Again, note that there is no ORM-generated code.</p>
  
  <pre><code>private class Product
  {
      [DatabaseMapping("ProductId")]
      public int Id { get; set; }
  
      [DatabaseMapping("Price")]
      public int PriceInCents { get; set; }
  }
  
  private string Convert(Expression expression)
  {
      // Some magic calls to .NET Framework code happen here.
      // [...]
  }
  
  private void TestConvert()
  {
      Expression&lt;Func&lt;Product, int, int, bool&gt;&gt; inPriceRange =
          (Product product, int from, int to) =&gt;
              product.PriceInCents &gt;= from &amp;&amp; product.PriceInCents &lt;= to;
  
      string actualQueryPart = this.Convert(inPriceRange);
  
      Assert.AreEqual("[Price] between @from and @to", actualQueryPart);
  }
  </code></pre>
  
  <p><strong>Where does the name <code>Price</code> come from in the expected query?</strong></p>
  
  <p>The name can be obtained through reflection by querying the custom <code>DatabaseMapping</code> attribute of <code>Price</code> property of <code>Product</code> class.</p>
  
  <p><strong>Where do names <code>@from</code> and <code>@to</code> come from in the expected query?</strong></p>
  
  <p>Those names are the actual names of the parameters of the expression.</p>
  
  <p><strong>Where does <code>between … and</code> come from in the expected query?</strong></p>
  
  <p>This is a possible result of a binary expression. Maybe EF or LINQ to SQL would, instead of <code>between … and</code> statement, stick with <code>[Price] &gt;= @from and [Price] &lt;= @to</code> instead. It's ok too, it doesn't really matter since the result is logically the same (I'm not mentioning performance).</p>
  
  <p><strong>Why there is no <code>where</code> in the expected query?</strong></p>
  
  <p>Because nothing indicates in the <code>Expression</code> that there must be a <code>where</code> keyword. Maybe the actual expression is just one of the expressions which would be combined later with binary operators to build a larger query to prepend with a <code>where</code>.</p>