I have a list of class A
, class A
contains a list of class B
. I want to operate on all instances of B
within all instances of class A
.
var myListOfA = new List<A>();
class A
{
public List<B> ListOfB;
}
How can I iterate over all B
i.e. foreach(var b in myListOfA.ListOfB){}
?
You can use SelectMany:
foreach(var b in myListOfA.SelectMany(a => a.ListofB))
See it in action at ideone.com.
another way that works well for how i think of nested objects is:
(from A objA in myListOfA
from B objB in objA.ListOfB
select objB);
this will "fan out" the list of b's within all the a's in the main list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With