title: How do I AddRange to a dictionary but filter out empty keys?
body: <p>The code in this question generates the following JSON.</p>
  
  <p>The code should exclude empty "Children" keys by virtue of the line <code>.Where(a =&gt; a.Values != null)</code>, but it doesn't work.</p>
  
  <p>Where can I place that where clause so that JSON doesn't include a bunch of empty "Children" arrays?</p>
  
  <p>Thanks for your help.</p>
  
  <pre><code>[{
          "NodeID" : 1,
          "NodeText" : "Country",
          "Children" : [{
                  "NodeID" : 3,
                  "NodeText" : "President",
                  "Children" : []
              }, {
                  "NodeID" : 4,
                  "NodeText" : "Population",
                  "Children" : []
              }, {
                  "NodeID" : 5,
                  "NodeText" : "State",
                  "Children" : [{
                          "NodeID" : 6,
                          "NodeText" : "Governor",
                          "Children" : []
                      }, {
                          "NodeID" : 7,
                          "NodeText" : "Population",
                          "Children" : []
                      }, {
                          "NodeID" : 8,
                          "NodeText" : "County",
                          "Children" : [{
                                  "NodeID" : 9,
                                  "NodeText" : "Population",
                                  "Children" : []
                              }
                          ]
                      }
                  ]
              }
          ]
      }, {
          "NodeID" : 2,
          "NodeText" : "Year",
          "Children" : []
      }
  ]
  </code></pre>
  
  <p>Here's the sample code that generates the JSON above:</p>
  
  <pre><code>public class Node
  {
      public int? ParentNodeID { get; set; }
      public int NodeID { get; set; }
      public string NodeText { get; set; }
  
      public Node(int? parentNodeID, int nodeID, string nodeText)
      {
          ParentNodeID = parentNodeID;
          NodeID = nodeID;
          NodeText = nodeText;
      }
  }
  
  public List&lt;Dictionary&lt;string, object&gt;&gt; BuildTree(int? parentNodeID = null, List&lt;Node&gt; exampleData = null)
  {
      // kickstart the recursion with example data
      if (exampleData == null)
      {
          exampleData = new List&lt;Node&gt;();
          exampleData.Add(new Node(null, 1, "Country"));
          exampleData.Add(new Node(null, 2, "Year"));
          exampleData.Add(new Node(1, 3, "President"));
          exampleData.Add(new Node(1, 4, "Population"));
          exampleData.Add(new Node(1, 5, "State"));
          exampleData.Add(new Node(5, 6, "Governor"));
          exampleData.Add(new Node(5, 7, "Population"));
          exampleData.Add(new Node(5, 8, "County"));
          exampleData.Add(new Node(8, 9, "Population"));
      }
  
      List&lt;Dictionary&lt;string, object&gt;&gt; result = new List&lt;Dictionary&lt;string, object&gt;&gt;();
  
      var nodes = exampleData.Where(a =&gt; a.ParentNodeID == parentNodeID).ToList();
  
      if (nodes != null)
      {
          result.AddRange(nodes
              .Select(a =&gt; new Dictionary&lt;string, object&gt; {
                  { "NodeID", a.NodeID},
                  { "NodeText", a.NodeText },
                  { "Children", BuildTree(a.NodeID, exampleData) }
              })
              .Where(a =&gt; a.Values != null) // this doesn't have any effect
              .ToList()
          );
      }
      return result;
  }
  </code></pre>