title: Linq: Sort child collection 2 levels down
body: <p>I'm currently stuck trying to figure out how to sort using the Event_Number field within my EventNumbers table.</p>
  
  <pre><code>var entries = await _context.Entries
              .Include(e =&gt; e.EntryEvents)
                  .ThenInclude(ee =&gt; ee.EventNumbers)
                      .ThenInclude(en =&gt; en.Event)
              .Include(e =&gt; e.EntrySwimmers)
                  .ThenInclude(es =&gt; es.Swimmers)
              .Include(e =&gt; e.EventType)
              .Where(e =&gt; e.MeetId == meetId &amp;&amp; e.LocationId == locationId)
              .OrderBy(p =&gt; p.EntryEvents
                  .Select(q =&gt; q.EventNumbers)
                  .OrderBy(c =&gt; c.Event_Number)
                  .Select(c =&gt; c.Event_Number)
                  .FirstOrDefault())
             .ToListAsync();
  </code></pre>
  
  <p>I've gone through multiple other posts on this subject and feel that I'm following them but the sort is not working. I'm hoping someone can point out my mistake. By the way, I'm VERY new to Linq/Asp.net. If this query is put together poorly, please let me know. I just know what I have now (other than the orderby) is working...</p>
  
  <p>Edit #2: So I've realized now, as a result of my interactions with @Gert-Arnold, that what I think I REALLY need is to sort my EntryEvents by the EventNumbers.Event_Number field, not to sort the EventNumbers by Event_Number.</p>
  
  <p>The EntryEvents model uses the EventNumbers.Id field as a reference to that table and when I display the entries in my View, I use:</p>
  
  <pre><code>@foreach (var e in item.EntryEvents)
  {
      &lt;span&gt;Event Number: @Html.DisplayFor(eventitem =&gt; e.EventNumbers.Event_Number)&lt;/span&gt;
      &lt;span&gt; | &lt;/span&gt;
      &lt;span&gt;Event Name: @Html.DisplayFor(eventitem =&gt; e.EventNumbers.Event.EventName)&lt;/span&gt;
      &lt;br/&gt;
  }
  </code></pre>
  
  <p>For reference, my EntryEvent entity:</p>
  
  <pre><code>public class EntryEvent
  {
      public int EntryEventId { get; set; }
      public int EventNumberId { get; set; }
      public EventNumber EventNumbers { get; set; }
      public int EntryId { get; set; }
      public Entry Entry { get; set; }
      public int EntryTypeId { get; set; }
      public EntryType EntryTypes { get; set; }
  }
  </code></pre>