仅按时间划分的平均数据表日期时间行
本文关键字:时间 数据表 日期 划分 | 更新日期: 2023-09-27 18:35:23
我有一个 sproc,它在过去 15 天上午 7 点到晚上 7 点之间每 5 分钟返回一次值。返回类似于:
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 返回的数据保存在数据集内的数据表中。我希望做类似的事情,除了我缺少 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 天内每个 TIME 的值求平均值的最佳方法有任何建议?
谢谢! :)
无需以
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"])
};
然后,可以枚举结果并显示该时间的时间和平均值。