递归一个List,可自己根据需要改造为通用型。
程序员文章站
2023-12-28 21:01:58
实体类: ......
/// <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();
}
}