John Goering
John Goering

Reputation: 39059

Creating and adding TreeNodes to a Tree with LINQ?

Still trying to wrap my head around LINQ coming from the Java and Objective-C world. Is there a way to accomplish the following loop with LINQ?

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

Upvotes: 0

Views: 466

Answers (2)

Chamika Sandamal
Chamika Sandamal

Reputation: 24322

Try,

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

Upvotes: 2

Ric
Ric

Reputation: 13248

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

Upvotes: 2

Related Questions