Solved

C#  Help with Recursive function - Generic List

Posted on 2010-11-16
10
853 Views
Last Modified: 2013-11-25
Hi..
I have Generic list of  objects.   The object contains a Parent and Children object that can contain additional objects with children.   The List<> is bound to a treeview.

I need to write a recursive method that will loop through all the objects and there children and return the names of the objects..
Any ideas?
thx



0
Comment
Question by:JElster
  • 5
  • 3
  • 2
10 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34145512
Have a look at this post: http:Q_26610392#a34121687

The example I posted in that question uses a DataTable as the source, but could just as easily be used with a List<> of objects.
0
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 34145519
I did the link wrong...try this: http:Q_26610392.html#a34121687
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34145710
Here is a console application that does what you want.

DaTribe
class Program
{
	class Item
	{
		public string Name { get; set; }
		public List<Item> Children { get; set; }

		public Item()
		{
			Children = new List<Item>();
		}
	}

	static void Main(string[] args)
	{
		Item parent = new Item() { Name = "1"};
		parent.Children.Add(new Item() { Name = "2"});
		parent.Children.Add(new Item() { Name = "" });

		var names = GetNames(parent);

		Console.ReadKey();
	}

	static List<string> GetNames(Item item)
	{
		List<string> names = new List<string>();
		names.Add(item.Name);

		foreach(var child in item.Children)
		{
			var childNames = GetNames(child);
			names.AddRange(childNames);
		}

		return names;
	}
}

Open in new window

0
 
LVL 1

Author Comment

by:JElster
ID: 34146366
Hi..
If I include a propertiy called IsSelected.
How can I only return those with the IsSelected = true:

thanks
0
 
LVL 18

Accepted Solution

by:
Richard Lee earned 500 total points
ID: 34146790
Try this:


DaTribe
static List<string> GetNames(Item item)
{
	List<string> names = new List<string>();

        // You can do this
        if(item.IsSelected)
	      names.Add(item.Name);

        // You can also do this -- but this excludes checking
        // the parent item
	foreach(var child in item.Children.Where(x => x.IsSelected))
	{
		var childNames = GetNames(child);
		names.AddRange(childNames);
	}

	return names;
}

Open in new window

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 18

Expert Comment

by:Richard Lee
ID: 34147000
Actually this version does not work

item.Children.Where(x => x.IsSelected)

This will exclude children further down the tree that are selected if their parent is not selected.

DaTribe
0
 
LVL 1

Author Comment

by:JElster
ID: 34147667
Yeah... I just tried it... any ideas?
thanks
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34147925
I gave you two ideas before:

if(item.IsSelected)
              names.Add(item.Name);

This one works!

DaTribe
0
 
LVL 1

Author Comment

by:JElster
ID: 34148715
i only got one.......

static List<string> GetNames(Item item)
{
      List<string> names = new List<string>();

        // You can do this
        if(item.IsSelected)
            names.Add(item.Name);

        // You can also do this -- but this excludes checking
        // the parent item
      foreach(var child in item.Children.Where(x => x.IsSelected))
      {
            var childNames = GetNames(child);
            names.AddRange(childNames);
      }

      return names;
}
 
0
 
LVL 18

Expert Comment

by:Richard Lee
ID: 34148803
Ok I see where the confusion lies. In that one post it was either or rather than implementing both solutions.

1. if(item.IsSelected)
         names.Add(item.Name);

or

2. foreach(var child in item.Children.Where(x => x.IsSelected))

DaTribe
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now