Kje*_*ous 6 c# datetime dictionary list
我正在寻找一种最佳实践来计算列表中每个日期的次数.
现在,我有工作代码(刚刚测试过),但我认为我的方式不太好.
var dates = new List<DateTime>();
//Fill list here
var dateCounter = new Dictionary<DateTime, int>();
foreach (var dateTime in dates)
{
   if (dateCounter.ContainsKey(dateTime))
   {
      //Increase count
      dateCounter[dateTime] = dateCounter[dateTime] + 1;
   }
   else
   {
      //Add to dictionary
      dateCounter.Add(dateTime, 1);
   }
}
谁知道更好的解决方案?
dates.GroupBy(d => d).ToDictionary(g => g.Key, g => g.Count());