zoukankan      html  css  js  c++  java
  • Lambda 递归获取所有子节点

    public class Organization
        {
            public string OID { get; set; }
            public string FathOID { get; set; }
            public string OrganizationDesc { get; set; }
            
        }

    public static List<Organization> GetWithChildrenList(List<Organization> list, string organizationID)
            {
                Organization curr = list.Where(x => x.OID == organizationID).FirstOrDefault();
                List<Organization> childrenList = new List<Organization>();
                if (curr != null)
                {
                    childrenList = GetChildrenList(list, organizationID);
                    
                }
                childrenList.Add(curr);
                return childrenList;
            }

            public static List<Organization> GetChildrenList(List<Organization> list, string organizationID)
            {

                var query = list.Where(x => x.FathOID == organizationID).ToList();
                return query.Concat(query.SelectMany(t => GetChildrenList(list, t.OID))).ToList();

            }

    //测试代码

    List<Organization> organizations = new List<Organization>();
                Organization ot1 = new Organization(){ OID="001",FathOID="0",OrganizationDesc="公司1"};
                Organization ot2 = new Organization() { OID = "002", FathOID = "001", OrganizationDesc = "财务部" };
                Organization ot3 = new Organization() { OID = "003", FathOID = "001", OrganizationDesc = "仓储部" };
                Organization ot4 = new Organization() { OID = "002001", FathOID = "002", OrganizationDesc = "财务小组21" };
                Organization ot5 = new Organization() { OID = "002001001", FathOID = "002001", OrganizationDesc = "财务小组21财务小组21" };

                organizations.Add(ot1);
                organizations.Add(ot2);
                organizations.Add(ot3);
                organizations.Add(ot4);
                organizations.Add(ot5);

                List<Organization> tt = GetWithChildrenList(organizations, "002");

  • 相关阅读:
    无线放大器扩展后,无线网络上不了网的问题解决
    可变现净值
    三大PLM厂商
    CF #589 (Div. 2) D. Complete Tripartite 构造
    CF #589 (Div. 2)C. Primes and Multiplication 快速幂+质因数
    BZOJ 4025 二分图 LCT维护最大生成树
    BZOJ3791 作业 动态规划
    luogu 2943 [USACO09MAR]清理Cleaning Up 动态规划
    luogu 4909 [Usaco2006 Mar]Ski Lift 缆车支柱 动态规划
    SP1716 GSS3(线段树+矩阵乘法)
  • 原文地址:https://www.cnblogs.com/volts0302/p/12054184.html
Copyright © 2011-2022 走看看