获取 LINQ 列表中的所有子项

本文关键字:LINQ 列表 获取 | 更新日期: 2023-09-27 18:35:41

我有一个大型后端数据库,它具有我正在使用的以下类结构:

public class InputClass
{
    public int id { get; set; }
    public string text { get; set; }
    public string icon { get; set; }
    public int? parentId { get; set; }
}

如您所见,每个元素都可以有一个父 ID,最终会生成一个树形的信息列表,用户可以与之交互。

以以下示例数据为例:

var inputList = new List<InputClass>();
inputList.Add(new InputClass() { id = 1, text = "Item #1"});
inputList.Add(new InputClass() { id = 2, text = "Item #2" });
inputList.Add(new InputClass() { id = 3, text = "Item #3" });
inputList.Add(new InputClass() { id = 4, text = "SubItem #1", parentId = 1 });
inputList.Add(new InputClass() { id = 5, text = "SubItem #2", parentId = 1 });
inputList.Add(new InputClass() { id = 6, text = "SubItem #3", parentId = 2 });
inputList.Add(new InputClass() { id = 7, text = "Sub-Sub Item #1", parentId = 4 });

我想传递一个 ID # 并检索标有该父 ID 的所有元素的列表。例如,如果我的 ID 号为 1,则结果应如下所示:

ID  Name
4   Subitem #1
5   Subitem #2
7   Sub-Sub Item #1

如您所见,结果应返回 ID #1 下的所有内容,包括 ID 为 #7 的项目(即使它的父 ID 为 4,项目 #4 的父项是 #1)。

我希望以上是有意义的,关于如何在 LINQ 中实现这一目标的任何想法?

获取 LINQ 列表中的所有子项

Recursive方法。

public static IEnumerable<InputClass> Recursive(List<InputClass> items, int toplevelid)     
{
    List<InputClass> inner = new List<InputClass>();
    foreach (var t in items.Where(item=>item.parentId ==toplevelid))
    {
        inner.Add(t);
        inner = inner.Union(Recursive(items, t.id)).ToList();
    }       
    return inner;
}

工作Demo

 static class Program
    {
        public static void Main()
        {
            IEnumerable<InputClass> allItems = someBLL.GetAllItems();
            int? someParentNode = 1;
            var allChildItems = InputClass.GetAllChildNodesRecursivrly(someParentNode, allItems);
        }
    }
    public class InputClass
    {
        public int id { get; set; }
        public string text { get; set; }
        public string icon { get; set; }
        public int? parentId { get; set; }
        public static IEnumerable<InputClass> GetAllChildNodesRecursivrly(int? ParentId,IEnumerable<InputClass> allItems)
        {
            var allChilds = allItems.Where(i => i.parentId == ParentId);
            if (allChilds==null)
            {
                return new List<InputClass>();
            }
            List<InputClass> moreChildes = new List<InputClass>();
            foreach (var item in allChilds)
            {
                moreChildes.AddRange(GetAllChildNodesRecursivrly(item.id,allItems));
            }
            return allChilds.Union(moreChildes);
        }
    }

在这种情况下:

static bool IsParent(IEnumerable<InputClass> lc, InputClass ic, int? ParentID)
{
    return ic.parentId != null ? ic.parentId == ParentID || IsParent(lc, lc.First(o => o.id == ic.parentId), ParentID) : false;
}
int? id = 1; // Parent ID
foreach(var i in inputList)
{
    if(IsParent(inputList, i, id)))
    {
        // get this item
    }
}


其他方式:
static IEnumerable<InputClass> getAllChildren(IEnumerable<InputClass> lc, int? ParentID)
{
    foreach (var i in lc)
    {
        if (i.parentId == ParentID)
        {
            yield return i;
            foreach (var i2 in getAllChildren(lc, i.id)) yield return i2;
        }
    }
}
int? id = 1; // Parent ID
foreach (var i in getAllChildren(inputList,id))
{
    // Get this Item
}

我认为你需要写的只是一个lambda表达式。

var results = inputList.Where(x => x.parentId == 1)