title: Ordering Entity Framework items and child items for MVC view
body: <p>How can I sort a query from a DbSet and include child items which should also be sorted.</p>
  
  <p>Example:</p>
  
  <p>I have a model for scheduling orders.</p>
  
  <pre><code>public class Order
  {
    public virtual int Id { get; set; }
    public virtual int? SchedulingOrder { get; set; }
    public virtual int? WeekId { get; set; }
    public virtual Week Week { get; set; }
  }
  public class Week
  {
    public virtual int Id { get; set; }
    public virtual DateTime StartDate { get; set; }
    public virtual ICollection&lt;Order&gt; Orders { get; set; }
  }
  ...
  public DbSet&lt;Week&gt; Weeks { get; set; }
  public DbSet&lt;Order&gt; Orders { get; set; }
  </code></pre>
  
  <p>Then an action method</p>
  
  <pre><code>public ActionResult ShopSchedule()
  {
    return View(db.Weeks.OrderBy(w =&gt; w.StartDate)
                   .Include(w =&gt; w.Orders.OrderBy(o =&gt; o.SchedulingOrder))
                   .ToList());
  }
  </code></pre>
  
  <p>This doesn't work I think because of the nature of <code>Include</code>. Do I have to create a separate view model and map to it? Or is there some way to get around it right there in the query? There is some kind of syntax where people say <code>new { left = right, etc }</code> within the query?</p>
  
  <p>related questions:<br>
  <a href="https://stackoverflow.com/questions/3591726/ordering-entity-framework-sub-items-for-editorfor">Ordering Entity Framework sub-items for EditorFor</a><br>
  <a href="https://stackoverflow.com/questions/6419523/c-sharp-entity-framework-4-1-lambda-include-only-select-specific-included-valu">C# Entity Framework 4.1 Lambda Include - only select specific included values</a></p>