title: Search through HierarchicalData with recursion
body: <p>I am building a treeview with a list of ScanItem. The class of ScanItem is actually:</p>
  
  <pre><code>public class ScanItem
      {
          public string FullPath { get; set; }
          public string Name
          {
              get
              {
                  return Path.GetFileName(FullPath);
              }
  
          }
          public DateTime ModifiedDate { get; set; }
          public DateTime CreatedDate { get; set; }
          public FileAttributes Attributes { get; set; }
          public bool IsDirectory { get; set; }
  
  
          public string Extension
          {
              get
              {
                  if (IsDirectory)
                      return "Folder";
                  else
                      return Path.GetExtension(Name);
              }
          }
  
          public UInt64 Size { get; set; }            
      }
  </code></pre>
  
  <p>In order for me to create a treeview I needed to create two other classes in order to distinguish the folders and files in my treeview:</p>
  
  <pre><code>    public class ScanFile : ScanItem
      {
  
      }
      public class ScanDir : ScanItem 
      {
          public List&lt;ScanItem&gt; Items { get; set; }
          public ScanDir()
          {
              Items = new List&lt;ScanItem&gt;();
          }                
      }
  </code></pre>
  
  <p>Note that the class ScanFile is just like the ScanItem and the ScanDir class has an extra property called Items and will contain a list of items of itself.  </p>
  
  <p>So if I where to iterate through this direcotory (C:\Temp):<img src="https://i.stack.imgur.com/GVYCd.png" alt="enter image description here"></p>
  
  <p>my List will actually contain:</p>
  
  <p><img src="https://i.stack.imgur.com/OpW9C.png" alt="enter image description here"></p>
  
  <p>note that if I expand one ScanDir object I will get another List:</p>
  
  <p><img src="https://i.stack.imgur.com/69KLK.png" alt="enter image description here"></p>
  
  <p>in order to populate the following treeview:</p>
  
  <p><img src="https://i.stack.imgur.com/LdCPk.png" alt="enter image description here"></p>
  
  <p>So I was able to populate this list using recursion by searching for files and directories in a specific path.</p>
  
  <p>I just wanted to explain my situation because there are several places in the internet that enable you to filter a treeview and that is what I actually want to do. But it will be nice if I can iterate through each item in List and then remove it if some criteria is not met:</p>
  
  <p>I have actually tried using the following recursive method to filter my results. </p>
  
  <pre><code>public List&lt;ScanItem&gt; search(List&lt;ScanItem&gt; items)
      {
          var filter = new List&lt;ScanItem&gt;();
  
          foreach (var item in items)
          {
              if (!item.FullPath.Contains("stringIwantToLookFor")) continue;
              filter.Add(item);
              if (item.IsDirectory)
              {
                  search(((ScanDir)item).Items);                    
              }                
          }
  
          return filter;
      }
  </code></pre>
  
  <p>I think that if an item is found I need to add all the parent root directories and that's why it does not work. The reason why I want to build my own recursion method is because I want to be able to filter the treeview based on spesific criteria. </p>
  
  <p>EDIT:</p>
  
  <p>In other words if I want to have all the items that contain "X.txt" in my listview I want to just see:
  <img src="https://i.stack.imgur.com/AjAJw.png" alt="enter image description here">  </p>