title: AutoMapper Project().To() and sorting a child collection
body: <p>I have an object graph that I'm loading from a database using EF CodeFirst and AutoMapper into DTOs:-</p>
  
  <pre><code>public class Foo
  {
    public int Id { get; set; }
    public virtual ICollection&lt;Bar&gt; Bars { get; set; }
  }
  
  public class Bar
  {
    public int Id { get; set; }
    public int FooId { get; set; }
    public virtual Foo Foo { get; set; }
  
    public string Name { get; set; }
    public int SortOrder { get; set; }
  }
  
  public class FooDto
  {
    public IEnumerable&lt;BarDto&gt; Bars { get; set; }
  }
  
  public class BarDto
  {
    public string Name { get; set; }
    public int SortOrder { get; set; }
  }
  </code></pre>
  
  <p>My mappings look like:-</p>
  
  <pre><code>mapper.CreateMap&lt;Foo, FooDto&gt;();
  mapper.CreateMap&lt;Bar, BarDto&gt;();
  </code></pre>
  
  <p>So far, so good. I can grab the entities from my context and project to the DTO nicely:-</p>
  
  <pre><code>var foos = context.Foos.Project().To&lt;FooDto&gt;();
  </code></pre>
  
  <p>What I can't do with this approach, however, is sort the <code>Bars</code> by their <code>SortOrder</code> inside the IQueryable.</p>
  
  <p>If I try:-</p>
  
  <pre><code>mapper.CreateMap&lt;Foo, FooDto&gt;()
    .ForMember(
      x =&gt; x.Bars
      opt =&gt; opt.MapFrom(src =&gt; src.Bars.OrderBy(x =&gt; x.SortOrder)));
  mapper.CreateMap&lt;Bar, BarDto&gt;();
  var foos = context.Foos.Project().To&lt;FooDto&gt;();
  </code></pre>
  
  <p>I get an exception:-</p>
  
  <pre><code>System.InvalidOperationException: Sequence contains no elements
    at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
    at AutoMapper.MappingEngine.CreateMapExpression(Type typeIn, Type typeOut)
    ...
  </code></pre>
  
  <p>Seems this is related to <a href="https://github.com/AutoMapper/AutoMapper/issues/159">https://github.com/AutoMapper/AutoMapper/issues/159</a> - though I'm already using a complex type for the child collection. I guess CreateMapExpression doesn't support OrderBy on child collections?</p>
  
  <p>If I'm not using .Project().To() then I can sort the child collection easily:-</p>
  
  <pre><code>var model = context.Foos.Select(x =&gt; new FooDto()
  {
    Bars = x.Bars.OrderBy(y =&gt; y.SortOrder)
  });
  </code></pre>
  
  <p>but then I have to repeat the mapping wherever I want to use it, defeating the purpose of using AutoMapper.</p>
  
  <p>Curiously:-</p>
  
  <p>1) I can perform other (more complicated?) operations on the child collection and flatten those into my parent DTO no problem:-</p>
  
  <pre><code>mapper.CreateMap&lt;Foo, FooDto&gt;()
    .ForMember(
      x =&gt; x.AllBarsHaveAName,
      opt =&gt; opt.MapFrom(src =&gt;
        src.Bars.All(x =&gt; x.Name != null)));
  </code></pre>
  
  <p>2) I can <code>Mapper.Map&lt;FooDto&gt;(foo);</code> in memory just fine, and it'll sort the bars no problem.</p>
  
  <p><strong>It's possible to sort the child collection at the IQueryable level while still using .Project().To()?</strong></p>