Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 894
  • Last Modified:

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



0
JElster
Asked:
JElster
  • 5
  • 3
  • 2
1 Solution
 
Todd GerbertIT ConsultantCommented:
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
 
Todd GerbertIT ConsultantCommented:
I did the link wrong...try this: http:Q_26610392.html#a34121687
0
 
Richard LeeSoftware EnthusiastCommented:
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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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

thanks
0
 
Richard LeeSoftware EnthusiastCommented:
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
 
Richard LeeSoftware EnthusiastCommented:
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
 
JElsterAuthor Commented:
Yeah... I just tried it... any ideas?
thanks
0
 
Richard LeeSoftware EnthusiastCommented:
I gave you two ideas before:

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

This one works!

DaTribe
0
 
JElsterAuthor Commented:
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
 
Richard LeeSoftware EnthusiastCommented:
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

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now