title: Execution-Deferred IQueryable<T> from Dynamic Linq?
body: <p>I am using <a href="https://www.nuget.org/packages/System.Linq.Dynamic">Dynamic Linq</a> to perform some queries (sorry but it's my only option). As a result, I am getting an <code>IQueryable</code> instead of an <code>IQueryable&lt;T&gt;</code>. In my case, I want an <code>IQueryable&lt;Thing&gt;</code> where <code>Thing</code> is a concrete type.</p>
  
  <p>My query is as such:</p>
  
  <pre><code>public IQueryable&lt;Thing&gt; Foo(MyContext db)
  {
      var rootQuery = db.People.Where(x =&gt; x.City != null &amp;&amp; x.State != null);
      var groupedQuery = rootQuery.GroupBy("new ( it.City, it.State )", "it", new []{"City", "State"});
      var finalLogicalQuery = groupedQuery.Select("new ( Count() as TotalNumber, Key.City as City, Key.State as State )");
      var executionDeferredResults = finalLogicalQuery.Take(10); // IQueryable
  
      IQueryable&lt;Thing&gt; executionDeferredTypedThings = ??; // &lt;--- Help here!!!!
  
      return executionDeferredTypedThings;
  }
  </code></pre>
  
  <p>My Thing.cs:</p>
  
  <pre><code>public class Thing
  {
      public int TotalNumber { get; set; }
      public string City { get; set; }
      public string State { get; set; }
  }
  </code></pre>
  
  <p>Yes, I know the exact above thing can be done without Dynamic Linq but I have some variableness going on that I've simplified out of here. I can get it to work with my variableness if my return type is simply <code>IQueryable</code> but I can't figure out how to convert to <code>IQueryable&lt;Thing&gt;</code> while keeping it execution-deferred and while also keeping Entity Framework happy. I do have the dynamic <code>Select</code> always returning something (with the correct data) that <em>looks like</em> a <code>Thing</code>. But I simply can't figure how to return the <code>IQueryable&lt;Thing&gt;</code> and could use some help there. Thanks!!</p>
  
  <h2>Failed Attempt 1</h2>
  
  <p>Based on <strong>Rex M</strong>'s suggestion, I am now trying to use AutoMapper to solve this problem (although I am not committed to this approach and am willing to try other approaches). For the AutoMapper approach, I am doing it as such:</p>
  
  <pre><code>IQueryable&lt;Thing&gt; executionDeferredTypedThings = executionDeferredResults.ProjectTo&lt;Thing&gt;(); // &lt;--- Help here!!!!
  </code></pre>
  
  <p>But this results in an InvalidOperationException:</p>
  
  <blockquote>
    <p>Missing map from DynamicClass2 to Thing. Create using Mapper.CreateMap.</p>
  </blockquote>
  
  <p>The thing is, while I have defined <code>Thing</code>, I have not defined <code>DynamicClass2</code> and as such, I cannot map it.</p>
  
  <h2>Failed Attempt 2</h2>
  
  <pre><code>IQueryable&lt;Thing&gt; executionDeferredTypedThings = db.People.Provider.CreateQuery&lt;Thing&gt;(executionDeferredResults.Expression);
  </code></pre>
  
  <p>This gives an InvalidCastException and seems to be the same underlying problem that the above AutoMapper fail hits:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery'1[DynamicClass2]' to type 'System.Linq.IQueryable'1[MyDtos.Thing]'.</p>
  </blockquote>