title: Ordering by the many side of an entity framework join
body: <p>I'm trying to accomplish the following using Entity Framework 4.1 in .Net 4;</p>
  
  <pre><code>var context = new MyEntities();
  
  var results = context.Diners
      .OrderBy(i =&gt; i.MenuItems.Course.DisplayOrder)
      .ThenBy(i =&gt; i.MenuItems.DisplayOrder);
  </code></pre>
  
  <p>The issue here is that MenuItems is an ObjectSet (collection) and a collection does not have a Course (another joined table). A MenuItem (singular) has a course though. Hope it is obvious.</p>
  
  <p>The following is the SQL I would use to do this;</p>
  
  <pre><code>Select
      *
  From
      Diner As D
  Inner Join
      DinerSelection As DS -- This is the Many to Many that EF has abstracted away.
  On  D.DinerId = DS.DinerId
  Inner Join
      MenuItem As MI
  On  DS.MenuItemId = MI.MenuItemId
  Inner Join
      Course As C
  On  MI.CourseId = C.CourseId
  Order By
      C.DisplayOrder, MI.DisplayOrder
  </code></pre>
  
  <p>If it is not an easy thing to do in EF then I'll just do it as a view, although I'd rather not.</p>