Linq - works in list listings

I have a list of classes A , class A contains a list of classes B I want to work in all instances of B in all instances of class A

 var myListOfA = new List<A>(); class A { public List<B> ListOfB; } 

How can I foreach(var b in myListOfA.ListOfB){} over all B ie foreach(var b in myListOfA.ListOfB){} ?

+7
source share
2 answers

You can use SelectMany :

 foreach(var b in myListOfA.SelectMany(a => a.ListofB)) 

Take a look at the action at ideone.com .

+9
source

another way that works well for how I think of nested objects:

 (from A objA in myListOfA from B objB in objA.ListOfB select objB); 

this will β€œdisable” list b within all a in the main list.

+2
source

All Articles