Reputation: 210
how can I write lambda expression for the following nested foreach loops:
var temp= new List<Items>();
foreach (var item in dto.Items)
{
temp.Add(item);
foreach (var child in item.Children)
{
temp.Add(child);
}
}
Upvotes: 1
Views: 1117
Reputation: 12846
dto.Items.SelectMany(item => item.Children).Concat(dto.Items);
should do it.
Edit:
As xanatos mentioned, if you want to have the same order as your loops produce, you should use this instead:
dto.Items.SelectMany(item => new[] { item }.Concat(item.Children))
Upvotes: 5