• 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");

  • 相关阅读:
    docker学习笔记1-- 用Toolbox安装Docker--介绍Docker Machine
    IDEA中文出现乱码解决
    hadoop本地运行与集群运行
    关于IDEA导出项目jar包/runnable jar
    IDEA 添加jar包的三种方式(重点:new uer Libraries)
    windows下客户端开发hdf--环境搭建
    junit在idea中的使用(1)--理论篇
    word的"bug"
    第4章 控制执行流程
    第3章 操作符
  • 原文地址:https://www.cnblogs.com/volts0302/p/12054184.html
Copyright © 2020-2023  润新知