Mid From PDF LINQ LINQ

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.

More from LINQ Tutorial

All questions for this course
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details