人民医院API

Tree.cs 940B

12345678910111213141516171819202122232425262728293031
  1. using System.Collections.Generic;
  2. using System.Text;
  3. namespace RMYY_CallCenter_Api.Utility
  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, string 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. entity.children = TreeRecursion(data, entity.id);
  22. newList.Add(entity);
  23. }
  24. }
  25. return newList;
  26. }
  27. }
  28. }