title: How to convert to generic code?
body: <p>I am trying to make a treeView that have items in the following order.</p>
  
  <pre><code>+ Continent
      + Country
          + Province
              + Territory
  </code></pre>
  
  <p>And, I have a generic class to hold the treeView Items data.</p>
  
  <pre><code>public class TreeViewContinents
  {
      public List&lt;TreeViewContinents&gt; Children { get; set; }    
      public TreeViewContinents Parent { get; set; }    
      public string Name { get; set; }    
      public string Content { get; set; }
  }
  </code></pre>
  
  <p><strong>Data:</strong></p>
  
  <pre><code>private readonly List&lt;Dictionary&lt;string, object&gt;&gt; continentsList = new List&lt;Dictionary&lt;string, object&gt;&gt;();
  
  /*
  continentsList[0] - "Continent", "Asia"
                      "Country", "Afghanistan"
                      "Province", "Kabul"
                      "Territory", "Bagrami"
                      "Language", "aaa"
                      "Culture", "bbb"
                  .
                  .               
                  .
                  .
  
  continentsList[n] - "Continent", "North America"
                      "Country", "Canada"
                      "Province", "Ontario"
                      "Territory", "Ottawa"
                      "Language", "aaa"
                      "Culture", "bbb
  
  */
  </code></pre>
  
  <p>String array based on which treeView Items needs to be generated.</p>
  
  <pre><code>var treeViewItemNames = new[] { "Continent", "Country", "Province", "Territory" };
  </code></pre>
  
  <p><strong>Code:</strong></p>
  
  <pre><code>List&lt;object&gt; Continents = this.continentsList.Where(
                              p_oDict =&gt; p_oDict.ContainsKey(treeViewItemNames[0]))
                              .Select( p_oDict =&gt; p_oDict[treeViewItemNames[0]])
                              .Distinct()
                              .ToList();
  
  var topItems = new List&lt;TreeViewContinents&gt;();
  
  foreach (object continent in Continents)
  {
      var level1Items = new TreeViewContinents { Name = treeViewItemNames[0], Content = continent.ToString() };
  
      List&lt;object&gt; Countries = this.continentsList.Where(
                              p_oDict =&gt; p_oDict.ContainsKey(treeViewItemNames[0])
                              &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[1])
                              &amp;&amp; p_oDict[treeViewItemNames[0]].Equals(continent)
                              .Select( p_oDict =&gt; p_oDict[treeViewItemNames[1]])
                              .Distinct()
                              .ToList();
  
      Countries.Sort((p_oU1, p_oU2) =&gt; string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
      foreach (object country in Countries)
      {
          var level2Items = new TreeViewContinents { Name = treeViewItemNames[1], Content = country.ToString(), Parent = level1Items };
          level1Items.Children.Add(level2Items);
  
          List&lt;object&gt; Provinces = this.continentsList.Where(
                                 p_oDict =&gt; p_oDict.ContainsKey(treeViewItemNames[0])
                                          &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[1])
                                            &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[2])
                                            &amp;&amp; p_oDict[treeViewItemNames[0]].Equals(continent)
                                            &amp;&amp; p_oDict[treeViewItemNames[1]].Equals(country))
                                            .Select( p_oDict =&gt; p_oDict[treeViewItemNames[2]]).Distinct()
                                          .ToList();
  
          Provinces.Sort((p_oU1, p_oU2) =&gt; string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
          foreach (object province in Provinces)
          {
              var level3Items = new TreeViewContinents { Name = treeViewItemNames[2], Content = province.ToString(), Parent = level2Items };
              level2Items.Children.Add(level3Items);
  
              List&lt;object&gt; Territories = this.continentsList.Where(
                                 p_oDict =&gt; p_oDict.ContainsKey(treeViewItemNames[0])
                                          &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[1])
                                          &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[2])
                                          &amp;&amp; p_oDict.ContainsKey(treeViewItemNames[3])
                                          &amp;&amp; p_oDict[treeViewItemNames[0]].Equals(continent)
                                          &amp;&amp; p_oDict[treeViewItemNames[1]].Equals(country)
                                          &amp;&amp; p_oDict[treeViewItemNames[2]].Equals(province)).Select(
                                          p_oDict =&gt; p_oDict[treeViewItemNames[3]])
                                          .Distinct()
                                          .ToList();
              Territories.Sort((p_oU1, p_oU2) =&gt; string.Compare(p_oU1.ToString(), p_oU2.ToString(), StringComparison.Ordinal));
              foreach (object territory in Territories)
              {
                  var level4Items = new TreeViewContinents { Name = treeViewItemNames[3], Content = territory.ToString(), Parent = level3Items };
                  level3Items.Children.Add(level4Items);
              }
          }
      }
  
      topItems.Add(level1Items);
  }
  </code></pre>
  
  <p>In the above code, I have four foreach loops to build a treeview. Is it possible to write a single loop to generate the treeView based on the the variable <code>treeViewItemNames</code>, so that in future If i change the variable like</p>
  
  <pre><code>var treeViewItemNames = new[] { "Continent", "Country", "Territory" };
  </code></pre>
  
  <p>it should generate treeView like this</p>
  
  <pre><code>+ Continent
      + Country
          + Territory
  </code></pre>
  
  <p>Ideas/ Suggestions please.</p>