我有一个递归数据结构,比如链表:
class Node
{
private Node next;
private int data;
// (...)
public Node Next
{
get
{
return next;
}
}
public int Data
{
get
{
return data;
}
}
}
我想做一个 LINQ 查询,它从列表的头部开始,然后遍历元素,即时收集数据。如何做到这一点?
最佳答案
我使用了两个帮助程序类来解析 TreeView 控件中的所有节点。
yield
关键字是如何根据您的需要进行调整的,您可能会受到启发。
internal static IEnumerable<TreeNode> Descendants(this TreeNodeCollection c)
{
foreach (var node in c.OfType<TreeNode>())
{
yield return node;
foreach (var child in node.Nodes.Descendants())
{
yield return child;
}
}
}
例如:
var allCheckedNodes = myTreeView.Nodes.Descendants().Where(c => c.Checked);
关于c# - 如何进行递归 LINQ 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24821653/