足力健后端,使用.netcore版本,合并1个项目使用

Tree.cs 977B

1234567891011121314151617181920212223242526272829303132
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace System.Common
  4. {
  5. public static class Tree
  6. {
  7. /// <summary>
  8. /// 递归算法
  9. /// </summary>
  10. /// <param name="data"></param>
  11. /// <param name="parentId"></param>
  12. /// <returns></returns>
  13. public static List<TreeModel> TreeRecursion(this List<TreeModel> data, int parentId = 0)
  14. {
  15. List<TreeModel> newList = new List<TreeModel>();
  16. List<TreeModel> item = data.FindAll(t => t.parentid == parentId);//data建议在调用此扩展方法前已经排序过
  17. if (item.Count > 0)
  18. {
  19. foreach (TreeModel entity in item)
  20. {
  21. var _list= TreeRecursion(data, entity.id);
  22. entity.children = _list.Count>0?_list:null;
  23. newList.Add(entity);
  24. }
  25. }
  26. return newList;
  27. }
  28. }
  29. }