【发布时间】:2022-01-08 00:14:40
【问题描述】:
public class Parent
{
public int ParentId { get; set; }
public IEnumerable<Child> Children { get; set; }
}
public class Child
{
public int ChildId { get; set; }
public bool Property { get; set; }
public Parent Parent { get; set; }
}
您如何急切地加载父类及其子类,但基于子类字段进行过滤?例如,我将如何根据Child.Property 是否为true 来选择Parent 记录?但我也想加载父级的所有子级,无论他们的 Property 属性是否设置为true。
我的尝试:
_context.Parents.Include(x => x.Children.Where(x => x.Property == true)).ToListAsync();
这似乎加载了每个父记录,但它加载了所有子项(无论它们的 Property 属性是否设置为 true),这些子项的子项的 Property 属性设置为 true。
【问题讨论】:
标签: c# asp.net-mvc entity-framework-core