Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Ordering Includes [duplicate]

I am trying to get something like the following to work:

_dbmsParentSections = FactoryTools.Factory.PdfSections                         .Include(x => x.Children.OrderBy(y => y.Order).ToList())                         .Include(x => x.Hint).Include(x => x.Fields)                         .Where(x => x.FormId == FormId && x.Parent == null)                         .OrderBy(o => o.Order)                         .ToList(); 

The part that causes the exception is:

.Include(x => x.Children.OrderBy(y => y.Order).ToList()) 

EDIT:

Upon further observation,

_dbmsParentSections.ForEach(x => x.Children = x.Children.OrderBy(y => y.Order).ToList()); 

did the job for me (after the initial Factory call and without the Children.OrderBy).

like image 853
Serj Sagan Avatar asked Mar 13 '13 06:03

Serj Sagan


1 Answers

It seems you cannot sort the children collection in your query. Either sort after the query or load the children in a second query.

Similar question and answer here

like image 200
Olav Nybø Avatar answered Oct 02 '22 20:10

Olav Nybø