递归linq查询以从给定的ID中获取父列表或子列表

问题描述 投票:0回答:1

我有一个具有父子关系的记录列表。根据下面的示例,如果我们从排序顺序最低的5开始,则顺序为5,7,4,3,1,6,2

现在,如果我通过3,我希望parentId列表为4,7,5,childId列表为1,6,2我可以按照以下方法通过递归函数来实现。somone可以帮助我通过linq查询而不是递归方法来实现这一点。

  public class ParentChildClass
        {
            public int Id { get; set; }
            public int? ChildId { get; set; }
            public int SortOrder { get; set; }

        }
    List<ParentChildClass>  parentChildList = new List<ParentChildClass>();
            public  void CreateParentChildList()
            {            
                parentChildList.Add(new ParentChildClass() { Id = 1, ChildId = 6 , SortOrder = 100});
                parentChildList.Add(new ParentChildClass() { Id = 2, ChildId = null, SortOrder = 100 });
                parentChildList.Add(new ParentChildClass() { Id = 3, ChildId = 1 , SortOrder = 100 });
                parentChildList.Add(new ParentChildClass() { Id = 4, ChildId = 3 , SortOrder = 100 });
                parentChildList.Add(new ParentChildClass() { Id = 5, ChildId = 7 , SortOrder = 10 });
                parentChildList.Add(new ParentChildClass() { Id = 6, ChildId = 2 , SortOrder = 100 });
                parentChildList.Add(new ParentChildClass() { Id = 7, ChildId = 4 , SortOrder = 100 });            
            }

public List<int> GetRecursiveParentIds (int parentId,List<int> parentIds)
        {
            CreateParentChildList();
            var parent = parentChildList.FirstOrDefault(i => i.ChildId == parentId);
            if (parent != null)
            {
                if (parentIds == null || !parentIds.Any())
                {
                    parentIds = new List<int>();
                }

                if (parent.ChildId != null)
                {
                    parentIds.Add((int)parent.Id);
                    GetRecursiveParentIds((int)parent.Id, parentIds);
                }
            }
            return parentIds;
        }

 public List<int> GetRecursiveChildIds(int childId, List<int> childIds)
        {
            CreateParentChildList();
            var child = parentChildList.FirstOrDefault(i => i.Id == childId);
            if (child != null)
            {
                if (childIds == null || !childIds.Any())
                {
                    childIds = new List<int>();
                }

                if (child.ChildId != null)
                {
                    childIds.Add((int)child.ChildId);
                    GetRecursiveChildIds((int)child.ChildId, childIds);
                }
            }
            return childIds;
        }

谢谢

c# linq parent
1个回答
0
投票
您可以尝试以下,

public List<int> GetOrder() { CreateParentChildList(); var tail = parentChildList.First(x => x.ChildId == null); int id = tail.Id; List<int> res = new List<int>() { tail.Id }; while(true){ var temp = parentChildList.FirstOrDefault(x => x.ChildId == id); if (temp == null) break; res.Add(temp.Id); id = temp.Id; } return res; } public List<int> GetRecursiveParentIds(int id) { var order = GetOrder(); order.Reverse(); var res = order.TakeWhile(x => x != id); res = res.Reverse(); return res.ToList(); } public List<int> GetRecursiveChildIds(int id) { var order = GetOrder().TakeWhile(x => x != id); var res = order.Reverse(); return res.ToList(); }

GetRecursiveChildIds(3)返回1,6,2

GetRecursiveParentIds(3)返回4,7,5

© www.soinside.com 2019 - 2024. All rights reserved.