using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DOAN.Common
{
public class Node
{
public int Id { get; set; }
public int? ParentId { get; set; }
}
public static class TreeNodeHelper
{
///
/// 获取指定节点的所有后代节点。
///
/// 所有节点的集合。
/// 根节点的 ID。
/// 所有后代节点的列表。
public static List GetDescendants(List nodes, int rootId)
{
var descendants = new List();
void FindChildren(Node node)
{
var children = nodes.Where(n => n.ParentId == node.Id).ToList();
foreach (var child in children)
{
descendants.Add(child);
FindChildren(child);
}
}
var rootNode = nodes.FirstOrDefault(n => n.Id == rootId);
if (rootNode != null)
{
FindChildren(rootNode);
}
return descendants;
}
}
}