LINQ Concat child lists in a list
var allObjectB = myList.SelectMany(x=>x.Children).ToList();
You can just use SelectMany
:
var result = mylist.SelectMany(a => a.Children).ToList();
SelectMany allows you to pass in a function that returns an IEnumerable<T>
, and it itself then returns an IEnumerable<T>
(unlike Select, which would return IEnumerable<IEnumerable<T>>
.
The result contains all of those enumerables concatenated together.