C# 使用Linq递归查询当前节点及所有子节点
C#Linq使用递归获取当前节点及所有子节点
LinqRecursive 类
C# 全选
/// <summary>
/// C#Linq使用递归获取当前节点及所有子节点
/// </summary>
public class LinqRecursive
{
public IEnumerable<sys_ProductFunctionAll> List { get; set; }
public List<sys_ProductFunctionAll> Get(string parentId)
{
return DoRecursive(parentId).ToList();
}
private IEnumerable<sys_ProductFunctionAll> DoRecursive(string parentId)
{
return List.Where(e => e.ParentFunctionID == parentId)
.SelectMany(e => new[] { e }.Concat(DoRecursive(e.FunctionID)))//递归获取子节点
.Union(List.Where(w => w.FunctionID == parentId)).ToList();//添加当前节点
}
}
使用方法
C# 全选
var nodes = new LinqRecursive { List = listAll }.Get(nodeId);
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网