我有一個簡單的對象列表「ProductGroups」(IQueryable)。 每個「ProductGroup」都有一個稱爲「Children」的ProductGroups集合,一個整數ParentId字段和一個布爾型「IsEnabled」字段。用LINQ過濾整個分層列表
public class ProductGroup
{
public int Id;
public int ParentId;
public ICollection<ProductGroup> Children;
public bool IsEnabled;
}
我希望能夠返回「IsEnabled」爲true的ProductGroups樹。
此刻,如果我做
ProductGroups.Where(x => x.IsEnabled)
這將返回功能的產品。如果我做
ProductGroups.Where(x => x.ParentId == null)
這將返回根。我希望能夠以儘可能最小的方式返回完整的樹(不包括禁用的項目)(即在查詢集合後不使用for循環)。
ProductGroup1 (IsEnabled == true)
|
--------- ProductGroup2 (IsEnabled == true)
| |
| ----------- ProductGroup4 (IsEnabled == false)
|
--------- ProductGroup4 (IsEnabled == false)
即返回ProductGroup1與1名兒童ProductGroup2
感謝
LINQ總是會返回一個平坦的IEnumerable,所以你要查找的內容(真的)是樹的根?真的嗎?如果是這樣,爲什麼不向ProductGroup添加一個返回已啓用子項(僅)的函數並將該函數與您的根一起使用?或者你真的試圖讓IEnumerable包含所有已啓用的節點及其子節點到樹葉? – Kevek