使用 Linq 表达式中的键创建 TreeNodes
我正在尝试使用键创建一个 TreeNode,但没有需要键和文本的 TreeNode 构造函数。我只找到以下解决方案:
TreeNode tn = new TreeNode("text node");
tn.Name = "keyNode";
treeView.Nodes.Add("keyNode", "text node");
但这些方法不适合我,因为我正在尝试使用 Linq 查询将新的 TreeNodes
添加到我的 treeView
中。
这是我理想中想做的事情:
treeView.Nodes.AddRange(
myListOfObject.Select(x => new TreeNode(x.somePropertyForKey,
x.somePropertyForText)).
ToArray<TreeNodes>());
我是否坚持使用 foreach
循环来创建 TreeNodes
或者您是否看到一种方法来执行此操作 - < em>是吗?
I am trying to create a TreeNode
with a key, but there is no constructor for TreeNode that takes a key and a text. I found only the following solutions:
TreeNode tn = new TreeNode("text node");
tn.Name = "keyNode";
treeView.Nodes.Add("keyNode", "text node");
But those ways do not suit me, as I am trying to add new TreeNodes
to my treeView
with a Linq query.
Here is what I would like to do ideally:
treeView.Nodes.AddRange(
myListOfObject.Select(x => new TreeNode(x.somePropertyForKey,
x.somePropertyForText)).
ToArray<TreeNodes>());
Am I stuck to use a foreach
loop to create the TreeNodes
or do you see a way to do this one-line-ish?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是新的初始化语法的用途
Thats what the new initialization syntax is for
应该是这样的。 (请检查大括号和语法)
您需要使用此构造函数
TreeNode(String, TreeNode[])
顺便说一句,如果这不起作用并且您没有访问数据库,您可以执行以下操作:
Should be something like that. (Please check the braces and syntax)
You would want to use this constructor
TreeNode(String, TreeNode[])
BTW, if that does not work and you are not hitting a database, you can do just the following: