Looping through list and executing function
<p>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.</p>

<p>This is what I have:</p>

<pre><code>public class MyObject
{
  public List&lt;NestedObject1&gt; ListNestedObject1 { get; set; }
  public List&lt;NestedObject2&gt; ListNestedObject2 { get; set; }

  public void ExecuteMethodsOfNestedObjectLists()
  {
    if (ListNestedObject1.Count &gt; 0) { from a in ListNestedObject1 a.MyMethod();}
    if (ListNestedObject2.Count &gt; 0) { from a in ListNestedObject2 a.MyMethod();}
  }
}
</code></pre>

<p>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.</p>

<p>How do I rewrite this to make it work?</p>

<p>Thanks.</p>
