實體類: ...
/// <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(); } }