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

  • 相关阅读:
    promise.all 发送多个请求
    解决分享里有中文,访问不了的问题
    elementui ztree 编辑的时候,回显半选状态的选项,用isLeaf判断
    倒叙数组查找数组对象中是否有该id值,将其去掉
    https强转
    Decorator学习(一)----- 基础知识
    MySQL的权限系统
    vue-cli 3.0.1 详细入门教程
    vue-cli4.5创建vue项目
    [记录] JavaScript 中的事件(Event对象、事件源对象、事件流、事件绑定)【转发】
  • 原文地址:https://www.cnblogs.com/volts0302/p/12054184.html
Copyright © 2011-2022 走看看