我是 LINQ 的新手,如果这是一个愚蠢的问题,请原谅我,但根据我对 LINQ 的了解,我相信我应该能够执行以下操作。 我想在 TreeView 中获取一组节点(在任何级别)并按字母顺序对其 sibling 进行排序。
我认为我可以做到以下几点:
//node is already selected
TreeNode parent = node.Parent;
TreeNodeCollection siblingNodes = node.Parent.Nodes;
siblingNodes = siblingNodes.OrderBy(x => x.Text);
由于 TreeNodeCollection 实现了 IEnumerable。但是编译器告诉我
System.Windows.Forms.TreeNodeCollection does not contain a definition for OrderBy and no extension method OrderBy accepting a first argument of type System.Windows.Forms.TreeNodeCollection could be found (are you missing a using directive or an assembly reference?)
(我正在使用 System.Linq)
那我误会了什么?
请您参考如下方法:
您是否缺少对 LINQ 的引用?
using System.Linq;
更新:
您需要定义一个 TreeViewNodeSorter 以便能够对节点进行排序。这篇文章将为您指明正确的方向:Sorting nodes of a TreeView
