title: Creating a dictionary from another dictionary using LINQ
body: <p>I have a dictionary of the type:</p>
  
  <pre><code>IDictionary&lt;foo, IEnumerable&lt;bar&gt;&gt; my_dictionary
  </code></pre>
  
  <p>bar class looks like this:</p>
  
  <pre><code>class bar
  {
      public bool IsValid {get; set;} 
  }
  </code></pre>
  
  <p>How can I create another dictionary with only those items that have IsValid = true.</p>
  
  <p>I tried this:</p>
  
  <pre><code>my_dictionary.ToDictionary( p=&gt; p.Key,
                              p=&gt; p.Value.Where (x =&gt; x.IsValid));
  </code></pre>
  
  <p>The problem with above code is that this creates a key with empty enumerable, if all the elements for that key were IsValid = false.</p>
  
  <p>for example:</p>
  
  <pre><code>my_dictionar[foo1] = new List&lt;bar&gt; { new bar {IsValid = false}, new bar {IsValid = false}, new bar {IsValid = false}};
  my_dictionary[foo2] = new List&lt;bar&gt; {new bar {IsValid = true} , new bar{IsValid = false};
  var new_dict = my_dictionary.ToDictionary( p=&gt; p.Key,
                              p=&gt; p.Value.Where (x =&gt; x.IsValid));
  // Expected new_dict should contain only foo2 with a list of 1 bar item.
  // actual is a new_dict with foo1 with 0 items, and foo2 with 1 item.
  </code></pre>
  
  <p>How do I get my expected.</p>