繁体   English   中英

实体框架由孙子实体过滤

[英]Entity Framework Filter by grandchildren entities

如果我有以下表格:
Parent :有ParentId
Child :有ChildIdParentId
Grandchild :有GrandchildIdChildIdQuantity

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

我和linq一起玩实体,生成类似的东西:

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

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

尝试这个:

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

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

这种方法表现不好......

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

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

if (!parent1.ChildrenReference.IsLoaded)    
     parent1.ChildrenReference.Load();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM