Foreach over a collection of IEnumerables
Posted
by sdr
on Stack Overflow
See other posts from Stack Overflow
or by sdr
Published on 2010-03-22T20:48:45Z
Indexed on
2010/03/22
20:51 UTC
Read the original article
Hit count: 304
I have 3 IEnumerables that I want to iterate over. I want to do something like this:
IEnumerable<Car> redCars = GetRedCars();
IEnumerable<Car> greenCars = GetGreenCars();
IEnumerable<Car> blueCars = GetBlueCars();
foreach(Car c in (redCars + greenCars + blueCars)) {
c.DoSomething();
}
...
The best way I can think of is:
...
List<Car> allCars = new List();
allCars.AddRange(redCars);
allCars.AddRange(greenCars);
allCars.AddRange(blueCars);
foreach(car in allCars) {
...
}
...
Is there a more concise way to do this? Seems like combinding IEnumberables should be trivial.
© Stack Overflow or respective owner