title: How to modify type parameter of Expression<Func<???, bool>>?
body: <p>I have an instance of the following:</p>
  
  <pre><code>Expression&lt;Func&lt;IRequiredDate, bool&gt;&gt;
  </code></pre>
  
  <p>I wish to convert it to an instance of the following, so it can be used to run a query in Entity Framework:</p>
  
  <pre><code>Expression&lt;Func&lt;TModel, bool&gt;&gt;
  </code></pre>
  
  <p>This will allow me to utilize a generic filtering query to any Model which implements IRequiredDate, eg.:</p>
  
  <pre><code>// In some repository function:
  var query = DbContext.Set&lt;Order&gt;()
       .FilterByDateRange(DateTime.Today, DateTime.Today);
  
  var query = DbContext.Set&lt;Note&gt;()
       .FilterByDateRange(DateTime.Today, DateTime.Today);
  
  var query = DbContext.Set&lt;Complaint&gt;()
       .FilterByDateRange(DateTime.Today, DateTime.Today);
  
  
  // The general purpose function, can filter for any model implementing IRequiredDate
  public static IQueryable&lt;TModel&gt; FilterByDate&lt;TModel&gt;(IQueryable&lt;TModel&gt; query, DateTime startDate, DateTime endDate) where TModel : IRequiredDate
  {
      // This will NOT WORK, as E/F won't accept an expression of type IRequiredDate, even though TModel implements IRequiredDate
      // Expression&lt;Func&lt;IRequiredDate, bool&gt;&gt; dateRangeFilter = x =&gt; x.Date &gt;= startDate &amp;&amp; x.Date &lt;= endDate;
      // query = query.Where(dateRangeFilter);
  
      // This also WON'T WORK, x.Date is compiled into the expression as a member of IRequiredDate instead of TModel, so E/F knocks it back for the same reason:
      // Expression&lt;Func&lt;TModel, bool&gt;&gt; dateRangeFilter = x =&gt; x.Date &gt;= startDate &amp;&amp; x.Date &lt;= endDate;
      // query = query.Where(dateRangeFilter);
  
      // All you need is lov.... uh... something like this:
      Expression&lt;Func&lt;IRequiredDate, bool&gt;&gt; dateRangeFilter = x =&gt; x.Date &gt;= startDate &amp;&amp; x.Date &lt;= endDate;
      Expression&lt;Func&lt;TModel, bool&gt;&gt; dateRangeFilterForType = ConvertExpressionType&lt;IRequiredDate, TModel&gt;(dateRangeFilter); // Must convert the expression from one type to another
      query = query.Where(dateRangeFilterForType) // Ahhhh. this will work.
  
      return query;
  }
  
  public static ConvertExpressionType&lt;TInterface, TModel&gt;(Expression&lt;Func&lt;TInterface, bool&gt;&gt; expression)
  where TModel : TInterface // It must implement the interface, since we're about to translate them
  {
      Expression&lt;Func&lt;TModel, bool&gt;&gt; newExpression = null;
  
      // TODO: How to convert the contents of expression into newExpression, modifying the
      // generic type parameter along the way??
  
      return newExpression;
  }
  </code></pre>
  
  <p>I understand that they are different types and cannot be cast. However I am wondering if there is a way to create a new <code>Expression&lt;Func&lt;TModel, bool&gt;&gt;</code>, then rebuild it based on the contents of the <code>Expression&lt;Func&lt;IRequiredDate, bool&gt;&gt;</code> provided, switching any type references from <code>IRequiredDate</code> to <code>TModel</code> in the process.</p>
  
  <p>Can this be done?</p>