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
A
B
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){} ?
foreach(var b in myListOfA.ListOfB){}
You can use SelectMany :
foreach(var b in myListOfA.SelectMany(a => a.ListofB))
Take a look at the action at ideone.com .
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.