JElster
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
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
I did the link wrong...try this: http:Q_26610392.html#a34121687
Here is a console application that does what you want.
DaTribe
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;
}
}
ASKER
Hi..
If I include a propertiy called IsSelected.
How can I only return those with the IsSelected = true:
thanks
If I include a propertiy called IsSelected.
How can I only return those with the IsSelected = true:
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
item.Children.Where(x => x.IsSelected)
This will exclude children further down the tree that are selected if their parent is not selected.
DaTribe
ASKER
Yeah... I just tried it... any ideas?
thanks
thanks
I gave you two ideas before:
if(item.IsSelected)
names.Add(item.Name);
This one works!
DaTribe
if(item.IsSelected)
names.Add(item.Name);
This one works!
DaTribe
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;
}
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
1. if(item.IsSelected)
names.Add(item.Name);
or
2. foreach(var child in item.Children.Where(x => x.IsSelected))
DaTribe
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.