// // // using ERP.Framework.WebApi; namespace ERP.Framework.Utils { public class TreeUtil { public static List Build(List list) where T : TreeEntity { var result = new List(); var tempIdList = new List(); foreach (var item in list) { tempIdList.Add(item.Id); } foreach (var item in list) { if (!tempIdList.Contains(item.ParentId)) { RecursionFn(list, item); result.Add(item); } } if (result.Count == 0) { result = list; } return result; } private static List GetChildList(List list, T t) where T : TreeEntity { return list.Where(l => l.ParentId == t.Id).ToList(); } private static void RecursionFn(List list, T t) where T : TreeEntity { List childList = GetChildList(list, t); t.Children = childList; foreach (var tChild in childList) { if (GetChildList(list, t).Count > 0) { RecursionFn(list, tChild); } } } } }