4

如果我有以下表格:
Parent: has ParentId
Child: hasChildIdParentId
Grandchild: has GrandchildId, ChildIdandQuantity

检索有孙子数量大于 10 的父母列表的最佳方法是什么(例如)?

我玩了 linq to entity,生成了类似的东西:

context.Parent.Includes("Children").Include("GrandChildren").Where( ... )

但不确定语法,我想知道性能 - 包含加载所有对象吗?实现这一目标的最佳方法是什么?

4

2 回答 2

6

试试这个:

var query = context.Parents
                   .Where(p => p.Children.Any(
                          c => c.GrandChildren.Any(g => g.Quantity > 10));

Include确实会加载与加载的父母相关的所有子实体和孙实体。

于 2012-08-09T08:26:08.840 回答
3

这种方法的性能很差......

context.Parent.Includes("Children").Include("Children.GrandChildren").Where( ... )

如果您稍后需要孩子和孙辈,或者可能根本不需要它们,请尝试稍后加载它们:

if (!parent1.ChildrenReference.IsLoaded)    
     parent1.ChildrenReference.Load();
于 2012-08-09T08:30:01.040 回答