/// <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();
        }
    }
扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。