[英]Entity Framework Filter by grandchildren entities
If I have the following tables: 如果我有以下表格:
Parent
: has ParentId
Parent
:有ParentId
Child
: has ChildId
and ParentId
Child
:有ChildId
和ParentId
Grandchild
: has GrandchildId
, ChildId
and Quantity
Grandchild
:有GrandchildId
, ChildId
和Quantity
Whats the best approach to retrieve a list of Parents where they have a grandchild with quantity greater than 10 (for example)? 什么是检索父母列表的最佳方法,他们有一个数量大于10的孙子(例如)?
I played with linq to entity, generating something like: 我和linq一起玩实体,生成类似的东西:
context.Parent.Includes("Children").Include("GrandChildren").Where( ... )
But wasn't sure about the syntax, and I wonder about performance- do the includes load up all objects? 但不确定语法,我想知道性能 - 包括加载所有对象吗? What's the best way to accomplish this? 实现这一目标的最佳方法是什么?
Try this: 尝试这个:
var query = context.Parents
.Where(p => p.Children.Any(
c => c.GrandChildren.Any(g => g.Quantity > 10));
Include
will indeed load all child and grandchild entities related to loaded parents. Include
确实会加载与加载的父项相关的所有子孙实体。
The performance is bad with this approach... 这种方法表现不好......
context.Parent.Includes("Children").Include("Children.GrandChildren").Where( ... )
If you need the children and grandchildren at a later point or maybe don't need them at all, try to load them later with: 如果您以后需要孩子和孙子或根本不需要它们,请尝试稍后加载它们:
if (!parent1.ChildrenReference.IsLoaded)
parent1.ChildrenReference.Load();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.