0

我想使用 Entityframework Plus访问Parent它唯一的活跃Children和活跃的Grand Children

关系
父 -> 子 -> 孙子

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true))
             .IncludeFilter(p=>p.Children.Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()

上面的查询不起作用。孩子不会被过滤。它返回所有孩子,包括不活动的孩子。但是 GrandChildren 会被过滤(但是我猜 Grand Childeren 会在内存中被过滤,而不是在 sql 中)

4

2 回答 2

1

您必须在第二次使用时在 Children 中包含过滤器IncludeFilter,否则,您将包含未过滤的 Children。

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true))
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true).Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()
于 2020-02-27T14:26:58.467 回答
0

这就是我最终使用的。这会创建 3 个 sql 语句

var parent = await _dbContext.Parent
             .IncludeFilter(p=>p.Children.Where(c=>c.IsActive == true).Select(c=>c.GrandChildren.Where(gc=>gc.IsActive ==true)))
             .Where(p=>p.ParnetID == 1234)
             .SingleOrDefaultAsync()
于 2020-02-27T15:51:31.640 回答