using cpf360.Common; using cpf360.ModelInfo; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace cpf360.BLL { public class TreeService<T> where T : TreeModel<T> { public static List<T> TreeLoop(List<T> datas) { return TreeLoop(datas, datas); } public static List<T> TreeLoop(List<T> basedatas, List<T> datas, long parentId = 0, string pathId = "0/", string pathName = "Root/") { var result = datas.Where(tm => tm.ParentId == parentId).ToList(); result.ForEach(item => { item.PathId = pathId.IsNullOrEmpty() ? "{0}/".FormatString(item.Id) : "{0}{1}/".FormatString(pathId, item.Id); item.PathName = pathName.IsNullOrEmpty() ? "{0}/".FormatString(item.Title) : "{0}{1}/".FormatString(pathName, item.Title); item.Childrens = basedatas.Where(tm => tm.ParentId == item.Id).ToList(); item.HasChildrens = item.Childrens.Count > 0; if (item.HasChildrens) { TreeLoop(basedatas, item.Childrens, item.Id, item.PathId, item.PathName); } }); return result; } } }
原文地址:https://www.cnblogs.com/chenyishi/p/8794232.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。