使用lambda从并发字典中选择记录

时间:2015-04-06 20:57:40

标签: c# vb.net lambda

我有一个带有datetime的并发字典作为键和整数值。每隔半小时有一个新条目,我有过去一周的数据,我想选择特定日期的顶部整数值。关于如何做到这一点的任何建议?

2 个答案:

答案 0 :(得分:2)

类似的东西:

var value = dictionary.Where(x => x.Key.Day == 5).Max(x=>x.Value);

答案 1 :(得分:1)

一种可能性是按日期对并发字典中的值进行分组:

var dic = new ConcurrentDictionary<DateTime, int>();
dic[new DateTime(2015, 4, 5, 7, 0, 1)] = 1;
dic[new DateTime(2015, 4, 5, 7, 0, 2)] = 2;
dic[new DateTime(2015, 4, 5, 7, 0, 3)] = 3;
dic[new DateTime(2015, 4, 6, 7, 0, 1)] = 5;
dic[new DateTime(2015, 4, 6, 7, 0, 2)] = 6;
dic[new DateTime(2015, 4, 6, 7, 0, 3)] = 7;

var searchDay = new DateTime(2015, 4, 5);

// Find the values corresponding to the specified day
var dates = dic.GroupBy(item => item.Key.Date).FirstOrDefault(x => x.Key == searchDay.Date);
if (dates != null)
{
    int max = dates.Max(x => x.Value);
    // That's the maximum value for the specified day
}