【问题标题】:Eager load filter based on child records基于子记录的急切负载过滤器
【发布时间】: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


    【解决方案1】:

    怎么样:

    _context.Parents
        .Include(x => x.Children)
        .Where(x => x.Children.Any(c => c.Property))
        .ToListAsync()
    

    【讨论】:

    • 向导状态;确认的。谢谢br0
    猜你喜欢
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2016-08-25
    相关资源
    最近更新 更多