|
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace BPA.MES.Base.Core
- {
- /// <summary>
- /// 名 称 :
- /// 创 建 人 :yangxiao
- /// 创建时间 : 2023/3/4 9:50:52
- /// 描 述 :
- /// </summary>
- public static class TreeExtensions
- {
- public static List<T> BuildTree<T>(this List<T> list, string rootId = "") where T : class, ITree<T>
- {
- string rootId2 = rootId;
- List<T> list2 = list;
- List<T> resul = null;
- if (list2.HasVal())
- {
- List<T> list3 = list2.Where((T e) => e.ParentId == rootId2).ToList();
- if (list3.HasVal())
- {
- resul = new List<T>();
- list3.ForEach(delegate (T it)
- {
- it.Children = list2.BuildTree(it.Id);
- resul.Add(it);
- });
- }
- }
-
- return resul;
- }
- public interface ITree<T> where T : class
- {
- string Id { get; set; }
-
- string ParentId { get; set; }
-
- List<T> Children { get; set; }
- }
- public static List<T> DeleteTreeNodes<T>(this List<T> list, Func<T, bool> condition) where T : class, ITree<T>
- {
- if (list.HasVal())
- {
- for (int i = 0; i < list.Count; i++)
- {
- T val = list[i];
- if (condition(val))
- {
- list.Remove(val);
- }
- else if (val.Children.HasVal())
- {
- val.Children.DeleteTreeNodes(condition);
- }
- }
- }
-
- return list;
- }
- }
- }
|