Entity Framework loading child collection with sort order

You cannot achieve it directly because neither eager or lazy loading in EF supports ordering or filtering.

Your options are:

  • Sort data in your application after you load them from database
  • Execute separate query to load child records. Once you use separate query you can use OrderBy

The second option can be used with explicit loading:

var parent = context.Parents.First(...);
var entry = context.Entry(parent);
entry.Collection(e => e.Children)
     .Query()
     .OrderBy(c => c.SortOrder)
     .Load();

Leave a Comment