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

  • 相关阅读:
    C++ fstream 用法
    Servlet详解(转)
    JSP&Servlet(转)
    我们工作是为了什么!
    常见C C++问题(转)
    一份诚恳的互联网找工作总结和感想(附:怎样花两年时间去面试一个人)
    第一篇
    洛谷p1064 金明的预算方法
    onload、DOMContentLoaded与性能问题
    jsbin本地部署
  • 原文地址:https://www.cnblogs.com/volts0302/p/12054184.html
Copyright © 2011-2022 走看看