title: How to filter tree structure?
body: <p>I have a tree of objects <code>MyNode</code>:</p>
  
  <pre><code>public class MyNode
  {
     public int Type { get; set; }
     public List&lt;MyNode&gt; Children { get; set; }
  }
  
  MyNode myRootNode;
  // initializing a tree structure
  </code></pre>
  
  <p>So I need to remove all nodes except </p>
  
  <ol>
  <li><p>Nodes that have <code>Type</code> property is equal to <code>int myType</code></p></li>
  <li><p>Nodes that contains in their child nodes at any level nodes with <code>Type</code> property is equal to <code>int myType</code></p></li>
  </ol>
  
  <p>My way:</p>
  
  <pre><code>bool Filter(MyNode node, MyNode parentNode, int type)
  {
     bool isFound = false;
  
     if (node.Type == type)
        isFound = true; // There's type
  
     foreach (MyNode child in node.Children)
     {
        if (FilterTree(child, node, type))
            isFound = true; // There is child node who has this type
     }
  
     // If there aren't this type neither any of its children has it
     if (!isFound)
     {
        parentNode.Children.Remove(node);
     }
  
     return isFound;
  }
  </code></pre>
  
  <p>I've got an exeption: <code>Collection was modified; enumeration operation may not execute.</code> I think that's because I deleting elements in the list. Is there a way to do it the right way ? Or what am I donig wrong ?</p>