HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Is there a better or more compact way of adding items in treeview using LINQ?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
linqmorewaybetteraddingitemstreeviewusingtherecompact

Problem

I am using the following code to add nodes in a treeview. Is there a better or more compact way of doing this by using LINQ?

foreach (Plan plan in this.IncomingPlan.Plans)
{
    foreach (Document doc in plan.Documents.Where(d => d.Name.Equals(this.DocumentName, StringComparison.OrdinalIgnoreCase)))
    {
        foreach (Author author in doc.Authors)
        {
            TreeNode treeNode = new TreeNode()
            {
                Text = author.Name,
                Type = NodeType.ParentNode,
                Tag = author
            };

            foreach (Book book in author.Books)
            {
                treeNode.Nodes.Add(new TreeNode()
                {                    
                    Text = book.Name,
                    Type = NodeType.ChildNode,
                    Tag = book
                });
            }

            this.treeView.Nodes.Add(treeNode);
        }
    }
}

Solution

Looks perfectly fine to me. That's the first time I've looked at your code and I was able to see quickly what it does.

There is no need to overly-compact things if it is going to make it a pain for someone else to understand in future.

Context

StackExchange Code Review Q#5301, answer score: 4

Revisions (0)

No revisions yet.