TreeExtensions.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Masuit.Tools.Models
  5. {
  6. /// <summary>
  7. /// 树形数据扩展
  8. /// </summary>
  9. public static class TreeExtensions
  10. {
  11. /// <summary>
  12. /// 过滤
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="items"></param>
  16. /// <param name="func"></param>
  17. /// <returns></returns>
  18. public static IEnumerable<T> Filter<T>(this IEnumerable<T> items, Func<T, bool> func) where T : ITree<T>
  19. {
  20. var results = new List<T>();
  21. foreach (var item in items.Where(i => i != null))
  22. {
  23. item.Children = item.Children.Filter(func).ToList();
  24. if (item.Children.Any() || func(item))
  25. {
  26. results.Add(item);
  27. }
  28. }
  29. return results;
  30. }
  31. /// <summary>
  32. /// 过滤
  33. /// </summary>
  34. /// <typeparam name="T"></typeparam>
  35. /// <param name="item"></param>
  36. /// <param name="func"></param>
  37. /// <returns></returns>
  38. public static IEnumerable<T> Filter<T>(this T item, Func<T, bool> func) where T : ITree<T>
  39. {
  40. return (new[] { item }).Filter(func);
  41. }
  42. /// <summary>
  43. /// 平铺开
  44. /// </summary>
  45. /// <typeparam name="T"></typeparam>
  46. /// <param name="items"></param>
  47. /// <returns></returns>
  48. public static IEnumerable<T> Flatten<T>(this IEnumerable<T> items) where T : ITree<T>
  49. {
  50. var result = new List<T>();
  51. foreach (var item in items)
  52. {
  53. result.Add(item);
  54. result.AddRange(item.Children.Flatten());
  55. }
  56. return result;
  57. }
  58. /// <summary>
  59. /// 所有子级
  60. /// </summary>
  61. public static ICollection<T> AllChildren<T>(this ITree<T> tree) where T : ITree<T> => GetChildren(tree);
  62. /// <summary>
  63. /// 所有父级
  64. /// </summary>
  65. public static ICollection<T> AllParent<T>(this ITree<T> tree) where T : ITree<T> => GetParents(tree);
  66. /// <summary>
  67. /// 是否是根节点
  68. /// </summary>
  69. public static bool IsRoot<T>(this ITree<T> tree) where T : ITree<T> => tree.Parent == null;
  70. /// <summary>
  71. /// 是否是叶子节点
  72. /// </summary>
  73. public static bool IsLeaf<T>(this ITree<T> tree) where T : ITree<T> => tree.Children.Count == 0;
  74. /// <summary>
  75. /// 深度层级
  76. /// </summary>
  77. public static int Level<T>(this ITree<T> tree) where T : ITree<T> => IsRoot(tree) ? 1 : Level(tree.Parent) + 1;
  78. /// <summary>
  79. /// 节点路径(UNIX路径格式,以“/”分隔)
  80. /// </summary>
  81. public static string Path<T>(this ITree<T> tree) where T : ITree<T> => GetFullPath(tree);
  82. private static string GetFullPath<T>(ITree<T> c) where T : ITree<T> => c.Parent != null ? GetFullPath(c.Parent) + "/" + c.Name : c.Name;
  83. /// <summary>
  84. /// 递归取出所有下级
  85. /// </summary>
  86. /// <param name="t"></param>
  87. /// <returns></returns>
  88. private static List<T> GetChildren<T>(ITree<T> t) where T : ITree<T>
  89. {
  90. return t.Children.Union(t.Children.Where(c => c.Children.Any()).SelectMany(tree => GetChildren(tree))).ToList();
  91. }
  92. /// <summary>
  93. /// 递归取出所有上级
  94. /// </summary>
  95. /// <param name="t"></param>
  96. /// <returns></returns>
  97. private static List<T> GetParents<T>(ITree<T> t) where T : ITree<T>
  98. {
  99. var list = new List<T>() { t.Parent };
  100. return t.Parent != null ? list.Union(GetParents(t.Parent)).ToList() : list;
  101. }
  102. }
  103. }