I have two entities called Category and Product with 1:n relation.
I want to get a Category with its childs that childs be in order.
This is my linq:
_db.Categories.Where(c => c.CategoryID == catID)
.Include(c => c.Products.OrderBy(p => p.ProductID))
.SingleOrDefault();
This query enforce with the below exception because of orderby.
The Include path expression must refer to a navigation property defined on the type. Use dotted paths for reference navigation properties and the Select operator for collection navigation properties. Parameter name: path
Include
has to reference a navigation property, which means you can't include anOrderBy()
. Instead of this:...you'll have to use this:
...to access an ordered list of
Products
for eachCategory
, you could add a property toCategory
like this:Eager loaded data cannot be ordered or filtered. That is linq-to-entities limitation and the only way how to order relations in the database is by using projection:
You can project to anonymous or custom type but you cannot project to mapped type (for example
Poll
).Another way is dividing this to two queries and use explicit loading: