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; }
}
您將如何急切加載父類及其子類,但根據子類欄位進行過濾?例如,我會怎么選擇Parent根據是否記錄Child.Property了true?但我也想加載父的所有孩子,無論他們的 Property 屬性是否設定為true.
我的嘗試:
_context.Parents.Include(x => x.Children.Where(x => x.Property == true)).ToListAsync();
這似乎加載了每個父記錄,但它加載了父級的所有子級(無論它們的 Property 屬性是否設定為 true),這些子級的 Property 屬性設定為 true。
uj5u.com熱心網友回復:
怎么樣:
_context.Parents
.Include(x => x.Children)
.Where(x => x.Children.Any(c => c.Property))
.ToListAsync()
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/371442.html
標籤:C# asp.net-mvc 实体框架核心
上一篇:如何在aspnetcorewebAPI上正確地將串列轉換為IQueryable
下一篇:引數1:無法從“System.Collections.Generic.List”轉換為“System.Collections.Generic.List”
