1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- // <author></author>
- // <date></date>
- // <description></description>
- using ERP.Framework.WebApi;
- namespace ERP.Framework.Utils
- {
- public class TreeUtil
- {
- public static List<T> Build<T>(List<T> list) where T : TreeEntity<T>
- {
- var result = new List<T>();
- var tempIdList = new List<long>();
- 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<T> GetChildList<T>(List<T> list, T t) where T : TreeEntity<T>
- {
- return list.Where(l => l.ParentId == t.Id).ToList();
- }
- private static void RecursionFn<T>(List<T> list, T t) where T : TreeEntity<T>
- {
- List<T> childList = GetChildList(list, t);
- t.Children = childList;
- foreach (var tChild in childList)
- {
- if (GetChildList(list, t).Count > 0)
- {
- RecursionFn(list, tChild);
- }
- }
- }
- }
- }
|