gpt4 book ai didi

c# - 枚举本身不是 IEnumerable 的集合?

转载 作者:IT王子 更新时间:2023-10-29 04:32:26 25 4
gpt4 key购买 nike

当您想递归枚举一个分层对象,根据某些条件选择一些元素时,有许多技术示例,例如“扁平化”,然后使用 Linq 进行过滤:就像在这里找到的那​​些:

link text

但是,当您枚举诸如 Form 的 Controls 集合或 TreeView 的 Nodes 集合之类的东西时,我一直无法使用这些类型的技术,因为它们似乎需要一个参数(扩展方法)是一个 IEnumerable 集合:传入 SomeForm.Controls 不会编译。

我发现的最有用的东西是:

link text

它确实为您提供了 Control.ControlCollection 的扩展方法,其结果为 IEnumerable,然后您可以将其与 Linq 一起使用。

我已经修改了上面的示例,可以毫无问题地解析 TreeView 的节点。

public static IEnumerable<TreeNode> GetNodesRecursively(this TreeNodeCollection nodeCollection)
{
foreach (TreeNode theNode in nodeCollection)
{
yield return theNode;

if (theNode.Nodes.Count > 0)
{
foreach (TreeNode subNode in theNode.Nodes.GetNodesRecursively())
{
yield return subNode;
}
}
}
}

这是我现在使用扩展方法编写的代码:

    var theNodes = treeView1.Nodes.GetNodesRecursively();

var filteredNodes =
(
from n in theNodes
where n.Text.Contains("1")
select n
).ToList();

而且我认为在传递约束条件的情况下可能有更优雅的方式来做到这一点。

我想知道是否可以通用地定义此类过程,以便:在运行时我可以将集合类型以及实际集合传递给通用参数,因此代码是独立于它是 TreeNodeCollection 还是 Controls.Collection。

我也很想知道是否有任何其他方式(更便宜?更快?)比第二个链接(上面)所示的方式更能以 Linq 可用的形式获取 TreeNodeCollection 或 Control.ControlCollection。

Leppie 关于“链接到第一个(上文)的 SO 帖子中的 SelectMany 的评论似乎是一个线索。”

我对 SelectMany 的实验是:好吧,称之为“灾难”。 :)

感谢任何指点。我花了几个小时阅读我能找到的涉及这些领域的每一篇 SO 帖子,并漫无目的地进入诸如“y-combinator”之类的新奇事物。一个“谦卑”的经历,我可能会补充:)

最佳答案

这段代码应该可以解决问题

public static class Extensions
{
public static IEnumerable<T> GetRecursively<T>(this IEnumerable collection,
Func<T, IEnumerable> selector)
{
foreach (var item in collection.OfType<T>())
{
yield return item;

IEnumerable<T> children = selector(item).GetRecursively(selector);
foreach (var child in children)
{
yield return child;
}
}
}
}

这是一个如何使用它的例子

TreeView view = new TreeView();

// ...

IEnumerable<TreeNode> nodes = view.Nodes.
.GetRecursively<TreeNode>(item => item.Nodes);

更新:回应 Eric Lippert 的帖子。

这是使用 All About Iterators 中讨论的技术的一个大大改进的版本.

public static class Extensions
{
public static IEnumerable<T> GetItems<T>(this IEnumerable collection,
Func<T, IEnumerable> selector)
{
Stack<IEnumerable<T>> stack = new Stack<IEnumerable<T>>();
stack.Push(collection.OfType<T>());

while (stack.Count > 0)
{
IEnumerable<T> items = stack.Pop();
foreach (var item in items)
{
yield return item;

IEnumerable<T> children = selector(item).OfType<T>();
stack.Push(children);
}
}
}
}

我使用以下 benchmarking technique 做了一个简单的性能测试.结果不言自明。树的深度对第二种解决方案的性能影响很小;而第一个解决方案的性能会迅速下降,当树的深度变得太大时,最终会导致 StackOverflowException

benchmarking

关于c# - 枚举本身不是 IEnumerable 的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1815497/

25 4 0