title: Linq query : Translate local variable reference
body: <p>I have a situation where I need to translate the local variables reference in a linq query to its values.
  For this I'm using this class to do a translate local variable reference in a query :</p>
  
  <pre><code>public static class Evaluator 
  {
      public static Expression PartialEval(Expression expression, Func&lt;Expression, bool&gt; fnCanBeEvaluated) 
      {
          return new SubtreeEvaluator(new Nominator(fnCanBeEvaluated).Nominate(expression)).Eval(expression);
      }
  
      public static Expression PartialEval(Expression expression) 
      {
          return PartialEval(expression, Evaluator.CanBeEvaluatedLocally);
      }
  
      private static bool CanBeEvaluatedLocally(Expression expression)
      {
          return expression.NodeType != ExpressionType.Parameter;
      }
  
      class SubtreeEvaluator: ExpressionVisitor
      {
          HashSet&lt;Expression&gt; candidates;
  
          internal SubtreeEvaluator(HashSet&lt;Expression&gt; candidates) 
          {
              this.candidates = candidates;
          }
  
          internal Expression Eval(Expression exp) 
          {
              return this.Visit(exp);
          }
  
          protected override Expression Visit(Expression exp) 
          {
              if (exp == null) 
              {
                  return null;
              }
  
              if (this.candidates.Contains(exp)) 
              {
                  return this.Evaluate(exp);
              }
  
              return base.Visit(exp);
          }
  
          private Expression Evaluate(Expression e) 
          {
              if (e.NodeType == ExpressionType.Constant) 
              {
                  return e;
              }
  
              LambdaExpression lambda = Expression.Lambda(e);
              Delegate fn = lambda.Compile();
  
              return Expression.Constant(fn.DynamicInvoke(null), e.Type);
          }
      }
  
      class Nominator : ExpressionVisitor 
      {
          Func&lt;Expression, bool&gt; fnCanBeEvaluated;
          HashSet&lt;Expression&gt; candidates;
          bool cannotBeEvaluated;
  
          internal Nominator(Func&lt;Expression, bool&gt; fnCanBeEvaluated) 
          {
              this.fnCanBeEvaluated = fnCanBeEvaluated;
          }
  
          internal HashSet&lt;Expression&gt; Nominate(Expression expression) 
          {
              this.candidates = new HashSet&lt;Expression&gt;();
              this.Visit(expression);
              return this.candidates;
          }
  
          protected override Expression Visit(Expression expression) 
          {
              if (expression != null) 
              {
                  bool saveCannotBeEvaluated = this.cannotBeEvaluated;
                  this.cannotBeEvaluated = false;
                  base.Visit(expression);
  
                  if (!this.cannotBeEvaluated) 
                  {
                      if (this.fnCanBeEvaluated(expression)) 
                      {
                          this.candidates.Add(expression);
                      }
                      else 
                      {
                          this.cannotBeEvaluated = true;
                      }
                  }
  
                  this.cannotBeEvaluated |= saveCannotBeEvaluated;
              }
  
              return expression;
          }
      }
  } 
  </code></pre>
  
  <p>And after to translate local variable reference , is used like this :</p>
  
  <pre><code>public class DbQueryProvider : QueryProvider 
  {
      // …
  
      private string Translate(Expression expression) 
      {
          expression = Evaluator.PartialEval(expression);
          return new QueryTranslator().Translate(expression);
      }
  }
  </code></pre>
  
  <p>For example if I have this expression :</p>
  
  <pre><code>string ct = "London";
  var query = db.Customers.Where(c =&gt; c.City == ct);
  </code></pre>
  
  <p>the returned expression after translation will be :</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM (SELECT * FROM Customers) AS T WHERE (City = 'London')
  </code></pre>
  
  <p>The code is working ok , with all the types of variables , except of DateTime.
  In my database I save only the Date part ( without time ) , for example 02/03/2012.</p>
  
  <p>Now if I have this query :</p>
  
  <pre><code>DateTime dt1 = Convert.ToDateTime("01/01/2012");
  var query = db.Orders.Where(c =&gt; c.dt == dt1);
  </code></pre>
  
  <p>If I try to translate , the returned expression is :</p>
  
  <pre class="lang-sql prettyprint-override"><code>SELECT * FROM (SELECT * FROM Orders) AS T WHERE (dt = '01/01/2012 12:00:00 AM')
  </code></pre>
  
  <p>As you can see the translated query contains the time at the end , and my query everytime return no records.</p>
  
  <p>What can I do ?</p>