Select 使用 lambda 从并发字典中记录
Select records from concurrent dictionary using lambda
我有一个以日期时间为键和整数值的并发字典。每半小时有一个新条目,我有过去一周的数据,我想 select 特定日期的最高整数值。有关如何执行此操作的任何建议?
类似于:
var value = dictionary.Where(x => x.Key.Day == 5).Max(x=>x.Value);
一种可能性是按日期对并发字典中的值进行分组:
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
}
我有一个以日期时间为键和整数值的并发字典。每半小时有一个新条目,我有过去一周的数据,我想 select 特定日期的最高整数值。有关如何执行此操作的任何建议?
类似于:
var value = dictionary.Where(x => x.Key.Day == 5).Max(x=>x.Value);
一种可能性是按日期对并发字典中的值进行分组:
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
}