zoukankan      html  css  js  c++  java
  • 递归一个List<T>,可自己根据需要改造为通用型。

            /// <summary>
            /// 递归一个List<ProvinceOrg>
            /// </summary>
            /// <returns></returns>
            public void RecursionProvinceOrgs(List<ProvinceOrg> provinceOrgs, ref List<ProvinceOrg> _provinceOrgs)
            {
                if (provinceOrgs != null && provinceOrgs.Count() > 0)
                {
                    // 1、通过linq查询出children为空的。
                    var _provinceOrgs_ = provinceOrgs.Where(p => p.children.Count() == 0);
                    if (_provinceOrgs_.Count() > 0)
                    {
                        //2、将children为空的添加至_provinceOrgs
                        _provinceOrgs.AddRange(_provinceOrgs_);
                    }
    
                    // 处理剩余不为空的,父级的ORG
                    var _provinceOrgs_Surplus = provinceOrgs.Where(p => p.children.Count() > 0);
                    foreach (ProvinceOrg provinceOrg in _provinceOrgs_Surplus)
                    {
                        ProvinceOrg provinceOrgClone = provinceOrg.Clone();
                        _provinceOrgs.Add(provinceOrgClone);
                        RecursionProvinceOrgs(provinceOrg.children, ref _provinceOrgs);
                    }
                }
            }
    

     实体类:

        public class ProvinceOrg : ICloneable
        {
            public int orgId { get; set; }
            public int parentOrgId { get; set; }
            public int areaId { get; set; }
            public string areaCode { get; set; }
            public string orgName { get; set; }
            public string fullOrgName { get; set; }
            public string orgType { get; set; }
            public string state { get; set; }
            public string orgCode { get; set; }
            public int seq { get; set; }
            public string isBusDep { get; set; }
            public string depCateCode { get; set; }
            public string legalPerson { get; set; }
            public string contacts { get; set; }
            public string phone { get; set; }
            public string address { get; set; }
            public string orgFunctions { get; set; }
            public int num;
            public List<ProvinceOrg> children { get; set; }
    
            /// <summary>
            /// 克隆并返回一个对象。
            /// </summary>
            /// <returns></returns>
            public ProvinceOrg Clone()
            {
                ProvinceOrg p = (ProvinceOrg)this.MemberwiseClone();
                p.children = new List<ProvinceOrg>();
                return p;
            }
    
            object ICloneable.Clone()
            {
                return this.Clone();
            }
        }
  • 相关阅读:
    Django:同一个app支持多个数据库
    Django部署以及整合celery
    软件破解码等
    操作日志的设计小结by大熊
    用户-权限-组织-三员分立
    mysql调优
    笔试面试中常见的位运算用法
    Ubuntu系统下搭建Java平台
    All about Oracle Character Set
    各位技术大牛们的逆袭集锦!屌丝们都看过来!
  • 原文地址:https://www.cnblogs.com/netlws/p/10701640.html
Copyright © 2011-2022 走看看