Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

C# Help with Recursive function - Generic List

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



Avatar of Todd Gerbert
Todd Gerbert
Flag of United States of America image

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.
I did the link wrong...try this: http:Q_26610392.html#a34121687
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

Avatar of JElster

ASKER

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

thanks
ASKER CERTIFIED SOLUTION
Avatar of Avodah
Avodah
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of JElster

ASKER

Yeah... I just tried it... any ideas?
thanks
I gave you two ideas before:

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

This one works!

DaTribe
Avatar of JElster

ASKER

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