tags:

views:

37

answers:

2
public class MyClass {

   List<MyOtherClass> myInnerList;

}

Let's say I have the following declared somewhere else:

List<MyClass> myOuterlist;

How would I quickly generate a list of the myInnerLists in C# using VS2005?

+3  A: 

Why not a simple for loop?

List<List<MyOtherClass>> innerLists = new List<List<MyOtherClass>>();
for (int i = 0; i < myOuterList.Count; i++)
{
    innerLists.Add(myOuterList[i].myInnerList);
}
McAden
@McAden: I thought there might be some C# sugar to solve the problem.
Craig Johnston
@Craig, the syntactic sugar exists, it's called Linq. But it's not available in VS2005... Anyway, you could certainly make the code above look nicer, by using foreach instead of for
Thomas Levesque
A: 

if you wanted to flatten the lists into a list of MyOtherClass:

List<MyOtherClass> list = m.SelectMany(x => x.myInnerList).ToList();
Mark Heath
That's not the question being asked, and the OP is using VS2005, so Linq is not an option
Thomas Levesque