Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#  Help with Recursive function - Generic List

Posted on 2010-11-16
10
Medium Priority
?
889 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

715 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