title: Using a LINQ ExpressionVisitor to replace primitive parameters with property references in a lambda expression
body: <p>I'm in the process of writing a data layer for a part of our system which logs information about automated jobs that run every day - name of the job, how long it ran, what the result was, etc.</p>
  
  <p>I'm talking to the database using Entity Framework, but I'm trying to keep those details hidden from higher-level modules and I don't want the entity objects themselves to be exposed.</p>
  
  <p>However, I would like to make my interface very flexible in the criteria it uses to look up job information. For example, a user interface should allow the user to execute complex queries like "give me all jobs named 'hello' which ran between 10:00am and 11:00am that failed." Obviously, this looks like a job for dynamically-built <code>Expression</code> trees.</p>
  
  <p>So what I'd like my data layer (repository) to be able to do is accept LINQ expressions of type <code>Expression&lt;Func&lt;string, DateTime, ResultCode, long, bool&gt;&gt;</code> (lambda expression) and then behind the scenes convert that lambda to an expression that my Entity Framework <code>ObjectContext</code> can use as a filter inside a <code>Where()</code> clause.</p>
  
  <p>In a nutshell, I'm trying to convert a lambda expression of type <code>Expression&lt;Func&lt;string, DateTime, ResultCode, long, bool&gt;&gt;</code> to <code>Expression&lt;Func&lt;svc_JobAudit, bool&gt;&gt;</code>, where <code>svc_JobAudit</code> is the Entity Framework data object which corresponds to the table where job information is stored. (The four parameters in the first delegate correspond to the name of the job, when it ran, the result, and how long it took in MS, respectively)</p>
  
  <p>I was making very good progress using the <code>ExpressionVisitor</code> class until I hit a brick wall and received an <code>InvalidOperationException</code> with this error message:</p>
  
  <blockquote>
    <p>When called from 'VisitLambda', rewriting a node of type
    'System.Linq.Expressions.ParameterExpression' must return a non-null
    value of the same type. Alternatively, override 'VisitLambda' and
    change it to not visit children of this type.</p>
  </blockquote>
  
  <p>I'm completely baffled. Why the heck won't it allow me to convert expression nodes which reference parameters to nodes which reference properties? Is there another way to go about this?</p>
  
  <p>Here is some sample code:</p>
  
  <pre><code>namespace ExpressionTest
  {
      class Program
      {
          static void Main(string[] args)
          {
              Expression&lt;Func&lt;string, DateTime, ResultCode, long, bool&gt;&gt; expression = (myString, myDateTime, myResultCode, myTimeSpan) =&gt; myResultCode == ResultCode.Failed &amp;&amp; myString == "hello";
              var result = ConvertExpression(expression);
          }
  
          private static Expression&lt;Func&lt;svc_JobAudit, bool&gt;&gt; ConvertExpression(Expression&lt;Func&lt;string, DateTime, ResultCode, long, bool&gt;&gt; expression)
          {
              var newExpression = Expression.Lambda&lt;Func&lt;svc_JobAudit, bool&gt;&gt;(new ReplaceVisitor().Modify(expression), Expression.Parameter(typeof(svc_JobAudit)));
              return newExpression;
          }
      }
  
      class ReplaceVisitor : ExpressionVisitor
      {
          public Expression Modify(Expression expression)
          {
              return Visit(expression);
          }
  
          protected override Expression VisitParameter(ParameterExpression node)
          {
              if (node.Type == typeof(string))
              {
                  return Expression.Property(Expression.Parameter(typeof(svc_JobAudit)), "JobName");
              }
              return node;
          }
      }
  }
  </code></pre>