title: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary
body: <p>I receive the above error message when performing a unit test on a method.  I know where the problem is at, I just don't know why it's not present in the dictionary.</p>
  
  <p>Here is the dictionary:</p>
  
  <pre><code>var nmDict = xelem.Descendants(plantNS + "Month").ToDictionary(
      k =&gt; new Tuple&lt;int, int, string&gt;(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), k.Ancestors(plantNS + "Report").First().Attribute("Location").Value.ToString()),
      v =&gt; { 
               var detail = v.Descendants(plantNS + "Details").First();
               return new HoursContainer
               {
                   BaseHours = detail.Attribute("BaseHours").Value,
                   OvertimeHours = detail.Attribute("OvertimeHours").Value,
                   TotalHours = float.Parse(detail.Attribute("BaseHours").Value) + float.Parse(detail.Attribute("OvertimeHours").Value)
               };
           });
  
  var mergedDict = new Dictionary&lt;Tuple&lt;int, int, string&gt;, HoursContainer&gt;();
  
  foreach (var item in nmDict)
  {
      mergedDict.Add(Tuple.Create(item.Key.Item1, item.Key.Item2, "NM"), item.Value);
  }
  
  
  var thDict = xelem.Descendants(plantNS + "Month").ToDictionary(
      k =&gt; new Tuple&lt;int, int, string&gt;(int.Parse(k.Ancestors(plantNS + "Year").First().Attribute("Year").Value), Int32.Parse(k.Attribute("Month1").Value), k.Ancestors(plantNS + "Report").First().Attribute("Location").Value.ToString()),
      v =&gt; {
               var detail = v.Descendants(plantNS + "Details").First();
               return new HoursContainer
               {
                   BaseHours = detail.Attribute("BaseHours").Value,
                   OvertimeHours = detail.Attribute("OvertimeHours").Value,
                   TotalHours = float.Parse(detail.Attribute("BaseHours").Value) + float.Parse(detail.Attribute("OvertimeHours").Value)
               };
           });
  
  foreach (var item in thDict)
  {
      mergedDict.Add(Tuple.Create(item.Key.Item1, item.Key.Item2, "TH"), item.Value);
  }
          return mergedDict;                                 
  </code></pre>
  
  <p>}</p>
  
  <p>and here is the method that is being tested:</p>
  
  <pre><code>protected IList&lt;DataResults&gt; QueryData(HarvestTargetTimeRangeUTC ranges,
          IDictionary&lt;Tuple&lt;int, int, string&gt;, HoursContainer&gt; mergedDict)
  {            
      var startDate = new DateTime(ranges.StartTimeUTC.Year, ranges.StartTimeUTC.Month, 1);
      var endDate = new DateTime(ranges.EndTimeUTC.Year, ranges.EndTimeUTC.Month, 1);
      const string IndicatorName = "{6B5B57F6-A9FC-48AB-BA4C-9AB5A16F3745}";
  
      DataResults endItem = new DataResults();
      List&lt;DataResults&gt; ListOfResults = new List&lt;DataResults&gt;();                               
  
      var allData =
  
      (from vi in context.vDimIncidents
      where vi.IncidentDate &gt;= startDate.AddYears(-3) &amp;&amp; vi.IncidentDate &lt;= endDate
          select new
          {
              vi.IncidentDate,
              LocationName = vi.LocationCode,
              GroupingName = vi.Location,
              vi.ThisIncidentIs, vi.Location
          });
  
      var finalResults = 
  
              (from a in allData
              group a by new { a.IncidentDate.Year, a.IncidentDate.Month, a.LocationName, a.GroupingName, a.ThisIncidentIs, a.Location }
                  into groupItem
              select new 
              {
                  Year = String.Format("{0}", groupItem.Key.Year),
                  Month = String.Format("{0:00}", groupItem.Key.Month),
                  groupItem.Key.LocationName,
                  GroupingName = groupItem.Key.GroupingName,
                  Numerator = groupItem.Count(),
                  Denominator = mergedDict[Tuple.Create(groupItem.Key.Year, groupItem.Key.Month, groupItem.Key.LocationName)].TotalHours,  
                  IndicatorName = IndicatorName,                        
              }).ToList();
  
  
      for (int counter = 0; counter &lt; finalResults.Count; counter++)
      {
          var item = finalResults[counter];
          endItem = new DataResults();
          ListOfResults.Add(endItem);
          endItem.IndicatorName = item.IndicatorName;
          endItem.LocationName = item.LocationName;
          endItem.Year = item.Year;
          endItem.Month = item.Month;
          endItem.GroupingName = item.GroupingName;
          endItem.Numerator = item.Numerator;
          endItem.Denominator = item.Denominator;               
      }
  
      foreach(var item in mergedDict)
      {
          if(!ListOfResults.Exists(l=&gt; l.Year == item.Key.Item1.ToString() &amp;&amp; l.Month == item.Key.Item2.ToString()
                  &amp;&amp; l.LocationName == item.Key.Item3))
          {
              for (int counter = 0; counter &lt; finalResults.Count; counter++)
              {
                  var data = finalResults[counter];
                  endItem = new DataResults();
                  ListOfResults.Add(endItem);
                  endItem.IndicatorName = data.IndicatorName; 
                  endItem.LocationName = item.Key.Item3;
                  endItem.Year = item.Key.Item1.ToString();
                  endItem.Month = item.Key.Item2.ToString();
                  endItem.GroupingName = data.GroupingName; 
                  endItem.Numerator = 0;
                  endItem.Denominator = item.Value.TotalHours;
              }
          }
      }
      return ListOfResults;
  }
  </code></pre>
  
  <p>The error occurs here:</p>
  
  <pre><code> Denominator = mergedDict[Tuple.Create(groupItem.Key.Year, groupItem.Key.Month, groupItem.Key.LocationName)].TotalHours,  
  </code></pre>
  
  <p>I do not understand why it is not present in the key.  The key consists on an int, int, string (year, month, location) and that is what I have assigned it.</p>
  
  <p>I've looked at all of the other threads concerning this error message but I didn't see anything that applied to my situation. </p>
  
  <p>I was unsure of what tags to put on this but  from my understanding the dictionary was created with linq to xml, the query is linq to sql and it's all part of C# so I used all the tags.  if this was incorrect then I apologize in advance.</p>