title: Execute expression on another IQueryable source
body: <p>There are two classes with identical field structure: <code>WebMessage</code> and <code>WebMessageDto</code>.</p>
  
  <p>I have a DataGrid which can work with both classes. And I have a RIA Service which can provide only <code>WebMessageDto</code> because <code>WebMessage</code> has navigation properties and they cannot be serialized.</p>
  
  <p>Now, DataGrid should query RIA Service method returning <code>IQueryable&lt;WebMessageDto&gt;</code>. I need handle DataGrid's query and execute it on ORM's <code>Table&lt;WebMessage&gt;</code>, then transform result to <code>WebMessageDto</code> type and return it to DataGrid.</p>
  
  <p>I started to implement custom IQueryable interface and I can handle expression.</p>
  
  <pre><code>public class WebMessageQueryContext
  {
      // Executes the expression tree that is passed to it. 
      internal static object Execute(Expression expression, bool isEnumerable)
      {
          // DataContext.WebMessages is a ORM table which returns IQueryable&lt;WebMessage&gt;
          List&lt;WebMessage&gt; list = DataContext.WebMessages.Provider.Execute(expression);
  
          return WebMessageDto.ConvertFrom(list); // returning List&lt;WebMessageDto&gt;
      }
  }
  </code></pre>
  
  <p>The code above will make recursive calls to this method. And I found microsoft's sample where queryable source changes in ExpressionVisitor and now I change original source to ORM's table.</p>
  
  <pre><code>    protected override Expression VisitConstant(ConstantExpression c)
      {
          if (c.Type == typeof(WebMessageDtoQuerySource&lt;WebMessageDto&gt;))
              return Expression.Constant(DataContext.WebMessages);
  
          return c;
      }
  </code></pre>
  
  <p>I'm getting exception when I execute the expression:</p>
  
  <p><code>Expression of type 'System.Data.Linq.Table'1[WebMessage]' cannot be used for parameter of type 'System.Linq.IQueryable'1[WebMessageDto]' of method 'Int32 Count[WebMessageDto](System.Linq.IQueryable</code>1[WebMessageDto])'`</p>
  
  <p>There's no much information about IQueryableProviders and I don't know what to do... Who can answer, can I do it theoretically?</p>