我已实现以下层次结构接口
interface ITree<T>
{
// Incremential unique key
int Id { get; set; }
string Name { get; set; }
// Hierarchy classical pattern
T Parent { get; set; }
ICollection<T> Children { get; set; }
// Computed values
int Depth { get; }
// Hierarchy path with dotted notation (e.g.: 12.45.554.23,
// where each path segment is an Id)
string Path { get; set; }
}
class TreeItem : ITree<TreeItem>
{
public int Id { get; set; }
public string Name { get; set; }
public TreeItem Parent { get; set; }
public ICollection<TreeItem> Children { get; set; }
public string Path { get; set; }
public int Depth { get { return Path.Split('.').Length - 1; } }
}
这些项目是通过Entity Framework存储和检索的,因此我们可以假设所有关系字段都不是空且一致的:
Path
和Depth
始终是最新的item.Parent?.Parent?.Parent
)Children
字段也是递归工作。Path
和Depth
是首选方法,因为它不需要计算关系字段。考虑我有以下层次结构:
- A (Depth = 0)
-- B (Depth = 1)
-- C (Depth = 1)
- D (Depth = 0)
-- E (Depth = 1)
我的所有元素都是无序的平面数组,比方说[D,C,B,E,A]。 我想使用Linq表达式按以下方式对它们进行排序:
示例给出了2个深度级别,但我希望表达式遍历层次结构,无论其深度如何。
请注意,我的数据结构的级别和路径字段可用于实现此目的,因为每当添加,移动或移除项目时都会重建树的所有路径,并且使用简单的拆分计算深度字段('。')在路上。
测试样本:
var A = new TreeItem { Id = 1, Name = "A", Path = "1" };
var B = new TreeItem { Id = 2, Name = "B", Path = "1.2", Parent = A };
var C = new TreeItem { Id = 3, Name = "C", Path = "1.3", Parent = A };
var D = new TreeItem { Id = 4, Name = "D", Path = "4" };
var E = new TreeItem { Id = 5, Name = "E", Path = "4.5", Parent = D };
// populate children for the example.
// My actual code is automatic thanks to EF Inverse Relationship.
A.Children = new List<TreeItem> { B, C };
D.Children = new List<TreeItem> { E };
var listToSortHierarchically = new List<TreeItem> { D, C, B, E, A };
// I want the result of the hierarchical sort to be A B C D E
答案 0 :(得分:2)
好的,首先你应该添加以下约束
interface ITree<T>
where T : class, ITree<T>
{
// ...
}
因此,我们可以使用Parent
和Children
属性无需投射,安全地导航层次结构。
其次,我不是重新发明轮子,而是从我对How to flatten tree via LINQ?(以及其他人)的回答中重复使用通用树顺序遍历辅助方法:
public static partial class TreeUtils
{
public static IEnumerable<T> Expand<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> elementSelector)
{
var stack = new Stack<IEnumerator<T>>();
var e = source.GetEnumerator();
try
{
while (true)
{
while (e.MoveNext())
{
var item = e.Current;
yield return item;
var elements = elementSelector(item);
if (elements == null) continue;
stack.Push(e);
e = elements.GetEnumerator();
}
if (stack.Count == 0) break;
e.Dispose();
e = stack.Pop();
}
}
finally
{
e.Dispose();
while (stack.Count != 0) stack.Pop().Dispose();
}
}
}
将口袋中的助手放在口袋里,方法很简单:
partial class TreeUtils
{
public static IEnumerable<T> Ordered<T>(this IEnumerable<T> source, Func<IEnumerable<T>, IEnumerable<T>> order = null)
where T : class, ITree<T>
{
if (order == null) order = items => items.OrderBy(item => item.Name);
return order(source.Where(item => item.Parent == null))
.Expand(item => item.Children != null && item.Children.Any() ? order(item.Children) : null);
}
}
样本用法:
List<TreeItem> flatList = ...;
var orderedList = flatList.Ordered().ToList();
更新:仅使用Path
和Id
属性,这是相同的:
public static partial class TreeUtils
{
public static IEnumerable<T> Ordered<T>(this IEnumerable<T> source, Func<IEnumerable<T>, IEnumerable<T>> order = null)
where T : class, ITree<T>
{
if (order == null) order = items => items != null && items.Any() ? items.OrderBy(item => item.Name) : null;
var chldrenByParentId = source
.Select(item => new { item, path = item.Path.Split('.') })
.ToLookup(e => e.path.Length >= 2 ? int.Parse(e.path[e.path.Length - 2]) : (int?)null, e => e.item);
return (order(chldrenByParentId[null]) ?? Enumerable.Empty<T>())
.Expand(item => order(chldrenByParentId[item.Id]));
}
}