title: Replace parameter type in lambda expression
body: <p>I am trying to replace the parameter type in a lambda expression from one type to another.</p>
  
  <p>I have found other answers on stackoverflow i.e. <a href="https://stackoverflow.com/questions/11159697/replace-parameter-in-lambda-expression">this one</a> but I have had no luck with them.</p>
  
  <p>Imagine for a second you have a domain object and a repository from which you can retrieve the domain object.</p>
  
  <p>however the repository has to deal with its own Data transfer objects and then map and return domain objects:</p>
  
  <p>ColourDto.cs
  </p>
  
  <pre><code>public class DtoColour {
  
      public DtoColour(string name)
      {
          Name = name;
      }
  
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>DomainColour.cs
  </p>
  
  <pre><code>public class DomainColour {
  
      public DomainColour(string name)
      {
          Name = name;
      }
  
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Repository.cs
  </p>
  
  <pre><code>public class ColourRepository {
      ...
      public IEnumerable&lt;DomainColour&gt; GetWhere(Expression&lt;Func&lt;DomainColour, bool&gt;&gt; predicate)
      {
          // Context.Colours is of type ColourDto
          return Context.Colours.Where(predicate).Map().ToList();
      }
  }
  </code></pre>
  
  <p>As you can see this will not work as the predicate is for the domain model and the Collection inside the repository is a collection of Data transfer objects.</p>
  
  <p>I have tried to use an <code>ExpressionVisitor</code> to do this but cannot figure out how to just change the type of the <code>ParameterExpression</code> without an exception being thrown for example:</p>
  
  <p>Test scenario
  </p>
  
  <pre><code>public class ColourRepository {
      ...
      public IEnumerable&lt;DomainColour&gt; GetWhere(Expression&lt;Func&lt;DomainColour, bool&gt;&gt; predicate)
      {
          var visitor = new MyExpressionVisitor();
          var newPredicate = visitor.Visit(predicate) as Expression&lt;Func&lt;ColourDto, bool&gt;&gt;;
          return Context.Colours.Where(newPredicate.Complie()).Map().ToList();
      }
  }
  
  
  public class MyExpressionVisitor : ExpressionVisitor
  {
      protected override Expression VisitParameter(ParameterExpression node)
      {
          return Expression.Parameter(typeof(ColourDto), node.Name);
      }
  }
  </code></pre>
  
  <p>finally here is the exception:</p>
  
  <blockquote>
    <p>System.ArgumentException : Property 'System.String Name' is not
    defined for type 'ColourDto'</p>
  </blockquote>
  
  <p>Hope someone can help.</p>
  
  <p><strong>EDIT: Here is a <a href="https://dotnetfiddle.net/LQ6XE9" rel="noreferrer">dotnetfiddle</a></strong></p>
  
  <p>still doesnt work.</p>
  
  <p><strong>Edit: Here is a working <a href="https://dotnetfiddle.net/HlZgPX" rel="noreferrer">dotnetfiddle</a></strong></p>
  
  <p>Thanks <a href="https://stackoverflow.com/users/276083/eli-arbel">Eli Arbel</a></p>