0

我有一个存储过程,它在过去 5 天的早上 7 点到晚上 7 点之间每 15 分钟返回一次值。返回类似于这样:

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 分钟为增量,存储在 DateTimes 列表中

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 天内平均每个 TIME 的值的最佳方法,是否有人有任何建议?

谢谢!:)

4

1 回答 1

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"])
            };

然后,您可以枚举结果并显示该时间的时间和平均值。

于 2013-10-14T02:43:47.810 回答