views:

123

answers:

2

Sorry for the weird caption. What I'm trying to achieve is simple:

IEnumerable<IEnumerable<Foo>> listoflist;
IEnumerable<Foo> combined = listoflist.CombineStuff();

Example:

{{0, 1}, {2, 3}} => {0, 1, 2, 3}

I'm positive there is a Linq expression for this...

Sidenote: The lists may be large.

+3  A: 

SelectMany()

OK

leppie
+13  A: 

As leppie says, you want Enumerable.SelectMany. The simplest form would be:

 combined = listOfList.SelectMany(x => x);

In query expressions, SelectMany is called when you have more than one from clause, so an alternative would be:

 combined = from x in listOfList
            from y in x
            select y;
Jon Skeet
I wonder why MS didnt overload all the extension methods taking no parameters and using identity instead. Guess type inference wont work so nice then.
leppie
Yes, type inference would get tricky - at least pre-covariance. After that it would be simpler though.
Jon Skeet