title: Sorting nested collection in projection: Unable to cast object of type 'SortOp' to type 'ProjectOp'
body: <p>I'm using projection of query results to a custom type, which isn't a part of entity data model:</p>
  
  <pre><code>public sealed class AlgoVersionCacheItem : NotificationObject
  {
      public int OrderId { get; set; }
      public string OrderTitle { get; set; }
      public int? CurrentVersion { get; set; }
      public int CachedVersion { get; set; }
      public IEnumerable&lt;int&gt; AvailableVersions { get; set; }
  }
  </code></pre>
  
  <p>I want <code>AvailableVersions</code> to be sorted in descending order. Hence, I've tried to add sorting for <code>AvailableVersions</code> in projection:</p>
  
  <pre><code>        return someQueryable
              .Select(version =&gt; new AlgoVersionCacheItem
              {
                  OrderId = version.OrderId,
                  OrderTitle = version.Order.Title,
                  CurrentVersion = version.Order.CurrentAlgoVersionId,
                  CachedVersion = version.Id,
                  AvailableVersions = version
                      .Order
                      .AlgoVersions
                      .Where(v =&gt; (allowUncommittedVersions || v.Statuses.Any(s =&gt; s.AlgoVersionStatusListItemId == ModelConstants.AlgoVersionCommitted_StatusId)) &amp;&amp; v.Id != version.Id)
                      .OrderByDescending(v =&gt; v.Id) // this line will cause exception
                      .Select(v =&gt; v.Id)
              })
              .Where(item =&gt; item.AvailableVersions.Any())
              .OrderByDescending(item =&gt; item.OrderId)
              .ToArray();
  </code></pre>
  
  <p>With sorting, execution of the query throws an <code>System.Data.EntityCommandCompilationException</code> with <code>System.InvalidCastException</code> as inner exception:  </p>
  
  <blockquote>
    <p>Unable to cast object of type
    'System.Data.Entity.Core.Query.InternalTrees.SortOp' to type
    'System.Data.Entity.Core.Query.InternalTrees.ProjectOp'</p>
  </blockquote>
  
  <p>Without <code>.OrderByDescending(v =&gt; v.Id)</code> everything works fine.<br>
  Is this yet another feature, that isn't supported in Entity Framework, or I've missed something?</p>
  
  <p>P.S. I know, that I can sort items later at client side, but I'm wondering about sorting at the server side.</p>