I have an object model that contains several lists of other objects. I want to loop thought every object in the lists and execute a method on them. The method is called MyMethod and every object in the lists has this method in its class definition.
This is what I have:
public class MyObject
{
public List<NestedObject1> ListNestedObject1 { get; set; }
public List<NestedObject2> ListNestedObject2 { get; set; }
public void ExecuteMethodsOfNestedObjectLists()
{
if (ListNestedObject1.Count > 0) { from a in ListNestedObject1 a.MyMethod();}
if (ListNestedObject2.Count > 0) { from a in ListNestedObject2 a.MyMethod();}
}
}
I'm looking to test the length of each list of nested object and execute MyMethod over each element. I know I could do a foreach loop but I want to see how to use linq syntax to keep it short.
How do I rewrite this to make it work?
Thanks.