首页 > 其他 > 详细

递归一个List<T>

时间:2019-04-13 16:46:32      阅读:142      评论:0      收藏:0      [点我收藏+]

 递归代码

/// <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();
        }
    }

 

递归一个List<T>

原文:https://www.cnblogs.com/netlws/p/10701640.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!