I am trying to flatten a parent/child list, of the same <Person>
type. The difficulty here, is that I would
like to merge both the parents and children into a flat <Person>
list.
Closest I got was:
class Person
{
public string Name { get; set; }
public List<Person> Children { get; set; }
}
List<Person> parents = new List<Person>() {
new Person() {
Name = "parentA",
Children = new List<Person>() {
new Person() { Name = "childB" },
new Person() { Name = "childC" }
}
},
new Person() {
Name = "parentD",
Children = new List<Person>() {
new Person() { Name = "childE" },
new Person() { Name = "childF" }
}
}
};
var result = parents.SelectMany(parent => parent.Children
.Select(child => parent.Name + ", " + child.Name));
Which gives me the result:
parentA, childB
parentA, childC
parentD, childE
parentD, childF
What I'm looking for is to get a <Person>
list such as:
parentA
childB
childC
parentD
childE
childF
Keeping the order, or the performance is not important. But I would like, if possible, to stick to pure LINQ and LINQ methods.
Thanks,