title: KeyValuePair with repeated Keys
body: <p>I am making a LINQ query to obtain all values in 2 fields from a database, age and date. I want to display the 5 highest age figures on a chart. To do so, I was trying to store the age and date in 2 separate Lists, ageList and dateList. </p>
  
  <p>Initially when comparing the value against index to index in each List, the relation to the age and date would be correct. For example:</p>
  
  <p>age stored at ageList.ElementAt(0) would be the correct value for the date at dateList.ElementAt(0).</p>
  
  <p>But since I am going to get the 5 highest age figures, I would need to <strong>Sort</strong> the ageList list. By doing that I am going to lose the match between the 2 lists. </p>
  
  <p>I tried to store the data in a SortedDictionary instead where the Key is age and Value is date. Problem is it doesn't allow repeated Keys. In my case, I would need repeated keys since multiple age can be the same and date can be the same too. </p>
  
  <p>Is there any way around this? </p>
  
  <p>My code trying to store with a Dictionary. It would throw an exception when it is storing repeated Keys. </p>
  
  <pre><code>//Linq query
  var xChartData = from x in db.Person
                   select new { x.Age, x.Date };
  
  SortedDictionary&lt;double, DateTime&gt; topAgeDict = new SortedDictionary&lt;double, DateTime&gt;();
  
  //Storing in Dictionary
  foreach (var x in xChartData)
  {
      topAgeDict.Add(x.Age, x.Date); //Exception caused when repeated values
  }
  
  Dictionary&lt;string, string&gt; stringDict = new Dictionary&lt;string, string&gt;();
  
  //store data as string for chart purposes
  foreach (var x in topAgeDict)
  {
      stringDict.Add(x.Key.ToString(), x.Value.ToString());  
  }
  
  List&lt;string&gt; ages = new List&lt;string&gt;();
  List&lt;string&gt; dates = new List&lt;string&gt;();
  
  //storing the dictionary key and value in List for element access.
  ages = stringDict.Keys.ToList();
  dates = stringDict.Values.ToList();
  
  //to store only the top 5 results. This will be the data used in chart.
  List&lt;string&gt; topFiveAge = new List&lt;string&gt;();
  List&lt;string&gt; topFiveDate = new List&lt;string&gt;();
  
  for (int x=1; x &lt;= 5; x++)
  {
      topFiveAge.Add(ages.ElementAt(ages.Count - x));
      topFiveDate.Add(dates.ElementAt(dates.Count - x));
  }
  
  //Chart
  var topAgefilePath = Server.MapPath("~/Chart_Files/topAge.jpg");
              if (System.IO.File.Exists(topAgefilePath))
              {
                  System.IO.File.Delete(topAgefilePath);
              }
              var ageChart = new Chart(580, 400);
              ageChart.AddTitle("Top Age");
              ageChart.AddSeries(
                  chartType: "column",
                  xValue: topFiveDate,
                  yValues: topFiveAge
              );
              ageChart.Save(topAgefilePath); 
  </code></pre>