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();
            }
        }
  • 相关阅读:
    Vue入门系列(四)之Vue事件处理
    Vue入门系列(五)Vue实例详解与生命周期
    微信为啥不能直接下载.apk安装包
    Oracle行转列SQL
    MyISAM 和InnoDB区别
    jQuery easyui datagrid数据绑定
    js调用百度地图API创建地图,搜索位置
    python tornado框架使用
    python数据库连接池
    python操作数据库
  • 原文地址:https://www.cnblogs.com/netlws/p/10701640.html
Copyright © 2011-2022 走看看