Entity Framework Ordering Includes
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
The extension method Include
is a mere wrapper around DbQuery.Include
. Internally it does not execute the expressions but only parses them, i.e. it takes their member expressions and converts them to a path as string. The path is used as input for DbQuery.Include
.
It has been requested before to enhance the functionality of Include
, e.g. to allow partly loaded collections by including a Where
clause. Ordering could be another change request. But as you see, because of the internal working of Include
the whole mechanism will have to be re-engineered to implement such enhancements. I don't see it on the current road map so it may take a while...
Depending on the use case you might not need to load in separate query or sort afterwards.
In my case I needed them ordered for when looping in the view so I just ordered there
@foreach (var subObject in Object.SubObjects.OrderBy(x=>x.Order))