Solved

C#  Help with Recursive function - Generic List

Posted on 2010-11-16
10
865 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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…

821 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