C# 无限递归,无限层级,无限层级树
程序开发
2023-09-16 17:38:39
废话不多说直接上代码,先看 层级包装实体类
public class TreeVo{public string id { set; get; }public string name { set; get; }public object children { set; get; }}
下面是数据承载类
public class MemberRank{private string _mrankcode;private string _node="";private string _parentnode="";private string _mrankname;private string _short="";private int _delflag=0;
}
下面看结果
public CommonResult QueryTree(){List GetModelList = memberRankService.GetModelList();List treeVos = new List();foreach (var item in GetModelList){if (string.IsNullOrEmpty( item.parentNode)){TreeVo treeVo = new TreeVo();treeVo.id = item.MRankCode;treeVo.name = item.MRankName;treeVo.children = GetTreeVos(GetModelList, item.node);treeVos.Add(treeVo);}}return commonResult.success(treeVos);}/// /// 无限递归/// /// /// [NonAction]public List GetTreeVos(List GetModelList, string parentId) {List treeVos = new List();foreach (var gpml in GetModelList) {TreeVo treeVo = new TreeVo();treeVo.id = gpml.MRankCode;treeVo.name = gpml.MRankName;if (gpml.parentNode.Equals(parentId)){treeVo.children= GetTreeVos(GetModelList, gpml.node);treeVos.Add(treeVo);}}return treeVos;}
标签:
上一篇:
nginx 设置静态资源访问造成中文404问题
下一篇:
相关文章
-
无相关信息