title: How to specify the shape of results with WebApi2, OData with $expand
body: <p>I have a problem executing my AutoMapper mappings when using OData with specific <code>$select</code> or <code>$expand</code> values.</p>
  
  <p>Using the WebApi Action:</p>
  
  <pre><code>    public IQueryable&lt;BookInRequestDto&gt; Get(ODataQueryOptions&lt;BookInRequest&gt; query)
      {
          var results = query.ApplyTo(_context.BookInRequests) as IQueryable&lt;BookInRequest&gt;;
          var mappedResults = Mapper.Map&lt;IQueryable&lt;BookInRequest&gt;, IQueryable&lt;BookInRequestDto&gt;&gt;(results);
          return mappedResults;
      }
  </code></pre>
  
  <p>When I query: <code>api/Get</code>, I get an appropriate response, but a Document's <code>Properties</code> is set to <code>null</code> response containing documents properties set to null.</p>
  
  <p>When I query: <code>api/Get?$expand=Documents/Properties</code>, the response is an empty array.</p>
  
  <p>As I understand, this is because Select/Expand changes the shape of the response, so it no longer matches an IQueryable of BookInRequest, and instead returns an IQueryable.</p>
  
  <p>I'm happy to return that, except I need to be able to apply the AutoMapper Mappings.  Is there anything that can be done to enforce the shape of the query results?</p>
  
  <p>I have the following entities:</p>
  
  <pre><code>public class BookInRequest {
      //...
      public virtual ICollection&lt;BookInDocument&gt; Documents { get; set; }
  }
  
  public class BookInDocument {
      public ICollection&lt;BookInDocumentProperty&gt; Properties { get; set; }     
  }
  </code></pre>
  
  <p>With Corresponding DTO's that are pretty much identical, except for the <code>BookInDocumentDto</code>:</p>
  
  <pre><code>public class BookInDocumentDto {
      public dynamic Properties { get; set; }
  }
  </code></pre>
  
  <p>My Mapping definition is as follows:</p>
  
  <pre><code>Mapper.CreateMap&lt;BookInRequest, BookInRequestDto&gt;();
  Mapper.CreateMap&lt;BookInDocument, BookInDocumentDto&gt;()
          .ForMember(x =&gt; x.Properties, 
            y =&gt; y.MapFrom(z =&gt; 
                     DynamicHelpers.PropertiesAsDynamic(z.Properties)));
  </code></pre>