title: Linq - Get all keys from dictionary where all values in the value list are equal to criteria
body: <p>Another Question very similar to <a href="https://stackoverflow.com/questions/31637750/linq-deferred-execution-dictionary-output">my previous question</a></p>
  
  <p>The sample data looks as follows ...</p>
  
  <p><a href="https://i.stack.imgur.com/R6Oqs.gif" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/R6Oqs.gif" alt="enter image description here"></a></p>
  
  <blockquote>
  <pre><code>public class Data
  {
      public int Key        { get; set; }
      public DateTime? Date1 { get; set; }
      public DateTime? Date2 { get; set; }
      public DateTime? Date3 { get; set; }
  }
  </code></pre>
  </blockquote>
  
  <pre><code>Data[] table = 
              {
                  new Data {
                          Key = 1234, 
                          Date1 = new DateTime(2015, 08, 24), 
                          Date2= new DateTime(2015, 08, 24),
                          Date3= null
                      },
                  new Data
                      {
                          Key = 2134, 
                          Date1 = null, 
                          Date2 = new DateTime(2015, 08, 24), 
                          Date3 = null
                      },
                  new Data
                  { 
                      Key = 2134, 
                      Date1 = new DateTime(2015, 08, 24), 
                      Date2 = null, 
                      Date3 = null
                  },
                  new Data{ 
                      Key = 2345, 
                      Date1 = null, 
                      Date2 = null, 
                      Date3 = new DateTime(2015, 08, 24)
                  },
                  new Data{ 
                      Key = 3451, 
                      Date1 = new DateTime(2015, 08, 23), 
                      Date2 = null, 
                      Date3 = new DateTime(2015, 08, 24)
                  }
              };
  </code></pre>
  
  <blockquote>
  <pre><code>   var myDateValue = new DateTime(2015, 08, 24);
  </code></pre>
  </blockquote>
  
  <p>When I search for a particular date, say 2015-08-24, I only want to get the keys where all the dates corresponding to this key are equal to this particular date.</p>
  
  <p>For the above date search, I would like to get 1234, 2134 and 2345 but NOT 3451 as the dates in the list value corresponding to the key 3451 are not the same.</p>
  
  <p>I tried something along the lines ...</p>
  
  <pre><code>var dict = table
      .Select(row =&gt; new {row.Key, Dates = new[] {row.Date1, row.Date2, row.Date3}})
      .Where(r =&gt; r.Dates.All(d =&gt; d.HasValue &amp;&amp; d.Value == myDateValue))
      .SelectMany(row =&gt; row.Dates.Select(d =&gt; new {row.Key, Date=d}))
      .GroupBy(row =&gt; row.Key)
      .ToDictionary(g =&gt; g.Key, g =&gt; g.Select(r =&gt; r.Date).ToList());
  </code></pre>
  
  <p>It doesn't work :-( </p>
  
  <p>Form the Dictionary dict ... </p>
  
  <pre><code>Key     Value 
  1234    List&lt;DateTime&gt; {2015-08-24, 2015-08-24} 
  2134    List&lt;DateTime&gt; {2015-08-24, 2015-08-24} 
  2345    List&lt;DateTime&gt; {2015-08-24} 
  3451    List&lt;DateTime&gt; {2015-08-23, 2015-08-24}
  </code></pre>
  
  <p>and only select/output the keys 1234, 2134 and 2345 as all the dates in the list corresponding to 3451 are not the same date values</p>
  
  <p>Let me clarfiy further ...</p>
  
  <pre><code>    var myDictionary = new Dictionary&lt;int, List&lt;DateTimeOffset?&gt;&gt;
      {
          {1234, new List&lt;DateTimeOffset?&gt; {new DateTimeOffset(DateTime.Today), null, new DateTimeOffset(DateTime.Today)}},
          {2134, new List&lt;DateTimeOffset?&gt; {null, new DateTimeOffset(DateTime.Today), new DateTimeOffset(DateTime.Today)}},
          {2345, new List&lt;DateTimeOffset?&gt; {null, null, new DateTimeOffset(DateTime.Today)}},
          {3451, new List&lt;DateTimeOffset?&gt; {new DateTimeOffset(DateTime.Today).AddDays(-1), null, new DateTimeOffset(DateTime.Today)}}
      };
  </code></pre>
  
  <p>If I search by DateTimeOffset(DateTime.Today), I only want to select or extract the keys 1234, 2134 and 2345 and not the last one because not all values for this key are equal to the searched datetimeoffset value.</p>
  
  <p>Any help is greatly appreciated.</p>