我有一个sproc,在过去的5天内每隔15分钟返回17分钟到晚上7点之间的值。回报类似于:
TheDate TheValue
2013-10-06 07:15:00.000 0
2013-10-06 07:30:00.000 0
2013-10-06 07:45:00.000 0
2013-10-06 08:00:00.000 0
2013-10-06 08:15:00.000 0
2013-10-06 08:30:00.000 0
2013-10-06 08:45:00.000 0
2013-10-06 09:00:00.000 0
2013-10-06 09:15:00.000 0
2013-10-06 09:30:00.000 0
2013-10-06 09:45:00.000 0
2013-10-06 10:00:00.000 0
2013-10-06 10:15:00.000 0
2013-10-06 10:30:00.000 0
2013-10-06 10:45:00.000 0
2013-10-06 11:00:00.000 0
2013-10-06 11:15:00.000 0
2013-10-06 11:30:00.000 0
2013-10-06 11:45:00.000 0
2013-10-06 12:00:00.000 0
2013-10-06 12:15:00.000 0
2013-10-06 12:30:00.000 0
2013-10-06 12:45:00.000 0
2013-10-06 13:00:00.000 0
2013-10-06 13:15:00.000 0
2013-10-06 13:30:00.000 0
2013-10-06 13:45:00.000 0
2013-10-06 14:00:00.000 0
2013-10-06 14:15:00.000 0
2013-10-06 14:30:00.000 0
2013-10-06 14:45:00.000 0
2013-10-06 15:00:00.000 0
2013-10-06 15:15:00.000 0
2013-10-06 15:30:00.000 0
2013-10-06 15:45:00.000 0
2013-10-06 16:00:00.000 0
2013-10-06 16:15:00.000 0
2013-10-06 16:30:00.000 0
2013-10-06 16:45:00.000 0
2013-10-06 17:00:00.000 0
2013-10-06 17:15:00.000 0
2013-10-06 17:30:00.000 0
2013-10-06 17:45:00.000 0
2013-10-06 18:00:00.000 0
2013-10-06 18:15:00.000 0
2013-10-06 18:30:00.000 0
2013-10-06 18:45:00.000 0
2013-10-06 19:00:00.000 0
2013-10-07 07:15:00.000 0
2013-10-07 07:30:00.000 0
2013-10-07 07:45:00.000 0
2013-10-07 08:00:00.000 0
2013-10-07 08:15:00.000 0
2013-10-07 08:30:00.000 2
2013-10-07 08:45:00.000 11
2013-10-07 09:00:00.000 21
2013-10-07 09:15:00.000 28
2013-10-07 09:30:00.000 15
2013-10-07 09:45:00.000 11
2013-10-07 10:00:00.000 26
2013-10-07 10:15:00.000 1
2013-10-07 10:30:00.000 18
2013-10-07 10:45:00.000 31
2013-10-07 11:00:00.000 28
2013-10-07 11:15:00.000 22
2013-10-07 11:30:00.000 28
2013-10-07 11:45:00.000 32
2013-10-07 12:00:00.000 34
2013-10-07 12:15:00.000 29
2013-10-07 12:30:00.000 23
2013-10-07 12:45:00.000 0
2013-10-07 13:00:00.000 1
2013-10-07 13:15:00.000 20
2013-10-07 13:30:00.000 32
2013-10-07 13:45:00.000 30
2013-10-07 14:00:00.000 32
2013-10-07 14:15:00.000 21
2013-10-07 14:30:00.000 19
2013-10-07 14:45:00.000 23
2013-10-07 15:00:00.000 33
2013-10-07 15:15:00.000 3
2013-10-07 15:30:00.000 22
2013-10-07 15:45:00.000 42
2013-10-07 16:00:00.000 32
2013-10-07 16:15:00.000 34
2013-10-07 16:30:00.000 27
2013-10-07 16:45:00.000 18
2013-10-07 17:00:00.000 5
2013-10-07 17:15:00.000 0
2013-10-07 17:30:00.000 0
2013-10-07 17:45:00.000 1
2013-10-07 18:00:00.000 0
2013-10-07 18:15:00.000 0
2013-10-07 18:30:00.000 0
2013-10-07 18:45:00.000 0
2013-10-07 19:00:00.000 0
这是2天,实际的sproc将返回5天,但所有格式都是。
无论如何,我需要在返回的所有日子里获得每次的平均值。所以7:15,7:30等的平均值
我列出了所有独特时间,上午7点到晚上7点,以15分钟为增量,存储在日期时间列表中
List<DateTime> time = new List<DateTime>();
foreach (DateTime a in time)
Console.WriteLine(a.TimeOfDay);
来自sproc的返回数据保存在DataSet内部的DataTable中。我希望做一些与此类似的事情,除非我错过了Where子句中的一个关键组件,如果有人可以提供帮助,我将非常感激!
List<int> blah = new List<int>();
foreach (DateTime a in time)
{
blah.Add(Convert.ToInt32(ds.Tables[0].AsEnumerable()
.Where(w => /* **NOTE** */)
.Average(g => Convert.ToInt32(g["TheValue"]))));
}
**注意 - 我需要弄清楚要放到时间为7:15的所有行或者时间为7:30的所有行,无论日期如何。
那么,是否有人建议在所有5天内对每个时间段的值进行平均的最佳方法可能是什么?
谢谢! :)
答案 0 :(得分:3)
无需按15分钟的增量预先计算时间列表。如果您只想按照proc所返回的所有数据按时间进行平均,那么您可以使用这样的LINQ查询来执行此操作:
using System;
using System.Data;
using System.Linq;
...
var results = from row in ds.Tables[0].AsEnumerable()
group row by ((DateTime) row["TheDate"]).TimeOfDay
into g
select new
{
Time = g.Key,
AvgValue = g.Average(x => (int) x["TheValue"])
};
然后,您可以枚举结果并显示该时间的时间和平均值。