You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using OpenAuth.Repository.Core;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace OpenAuth.App.Base.Tree
|
|
|
|
|
{
|
|
|
|
|
public static class TreeExtensions
|
|
|
|
|
{
|
|
|
|
|
public static IEnumerable<TreeItemLong> BuildTree(this List<TreeItemLong> nodes, long? parentId = null)
|
|
|
|
|
{
|
|
|
|
|
var rootNodes = nodes
|
|
|
|
|
.Where(n => n.ParentId == parentId || (parentId == null && (n.ParentId == null || !nodes.Any(child => child.Id == n.ParentId))));
|
|
|
|
|
|
|
|
|
|
foreach (var rootNode in rootNodes)
|
|
|
|
|
{
|
|
|
|
|
yield return new TreeItemLong
|
|
|
|
|
{
|
|
|
|
|
Id = rootNode.Id,
|
|
|
|
|
Name = rootNode.Name,
|
|
|
|
|
ParentId = rootNode.ParentId,
|
|
|
|
|
Level = rootNode.Level,
|
|
|
|
|
Children = nodes.BuildTree(rootNode.Id).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static List<TreeItemLong> BuildTree1(this List<TreeItemLong> nodes, long? parentId = null)
|
|
|
|
|
{
|
|
|
|
|
var result = new List<TreeItemLong>();
|
|
|
|
|
|
|
|
|
|
foreach (var node in nodes.Where(n => n.ParentId == parentId))
|
|
|
|
|
{
|
|
|
|
|
var newNode = new TreeItemLong
|
|
|
|
|
{
|
|
|
|
|
Id = node.Id,
|
|
|
|
|
Name = node.Name,
|
|
|
|
|
ParentId = node.ParentId,
|
|
|
|
|
Children = nodes.BuildTree1(node.Id)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.Add(newNode);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|