How to flatten hierarchical data with SelectMany?
How to flatten hierarchical data with SelectMany?
var departmentsWithEmployees = new[]
new { Department = "IT", Employees = new [] {"Bob", "Charlie"}
new { Department = "HR", Employees = new [] {"Alice", "Eva"} }
var allEmployees = departmentsWithEmployees
.SelectMany(d => d.Employees);
foreach (var emp in allEmployees)
Console.WriteLine(emp);
Explanation:
SelectMany flattens nested collections into a single sequence.