EF Code First 预加载和 OrderBy 问题
我有两个名为“类别”和“产品”的实体,具有 1:n 关系。
我想要一个带有子项的类别,子项是有序的。
这是我的 linq:
_db.Categories.Where(c => c.CategoryID == catID)
.Include(c => c.Products.OrderBy(p => p.ProductID))
.SingleOrDefault();
由于 orderby,此查询强制执行以下异常。
包含路径表达式必须引用 到定义的导航属性 类型。使用点路径 参考导航属性和 用于集合的 Select 运算符 导航属性。参数名称: 路径
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
急切加载的数据无法排序或过滤。这是 linq-to-entities 的限制,在数据库中对关系进行排序的唯一方法是使用投影:
您可以投影到匿名或自定义类型,但不能投影到映射类型(例如
Poll
)。另一种方法是将其分为两个查询并使用显式加载:
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:
Include
必须引用导航属性,这意味着您不能包含OrderBy()
。而不是这个:...您必须使用这个:
...要访问每个
Category
的Products
有序列表,您可以将一个属性添加到 <代码>类别像这样: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: