Monday, March 22, 2010

Foreach over a collection of IEnumerables

Programmer Question

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.



Find the answer here

No comments:

Post a Comment

LinkWithin

Related Posts with Thumbnails