使用LINQ创建TreeNode并将其添加到树中?

时间:2013-06-20 07:54:16

标签: c# winforms linq

仍然试图围绕来自Java和Objective-C世界的LINQ。有没有办法用LINQ完成以下循环?

        foreach(MyData d in mydata)
        {
            TreeNode n = new TreeNode(d.DisplayName);
            this.myTree.Nodes.Add(n);
        }

2 个答案:

答案 0 :(得分:2)

myTree.Nodes.AddRange(myData.Select(n => new TreeNode(n.DisplayName)).ToArray());

答案 1 :(得分:2)

尝试,

myTree.Nodes.AddRange(myData.Select(n => new TreeNode(n.DisplayName)).ToArray());