0

每 5 分钟在 sql server 表中添加一行。这些字段是:DateTime timeMark,Int total。

使用实体框架,我想使用过去三个月的平均值填充一个涵盖整周五分钟值的新列表。

我将如何使用 Entity Framework 完成此任务?

4

1 回答 1

2

假设您的日志在“五分钟”上非常准确,并且我理解得很好,您想要一个 7 天 * 24 小时 * (60/5) 分钟的列表,那么 2016 年的结果?

//define a startDate
var beginningDate = <the date 3 month ago to start with>;

//get the endDate
var endDate = beginningDate.AddMonths(3);
var list = myTable.Where(m => m.TimeMark >= beginningDate && m.TimeMark <=endDate)
           //group by dayofWeek, hour and minute will give you data for each distinct day of week, hour and minutes
           .GroupBy(m => new {
               dayofWeek = SqlFunctions.DatePart("weekday", m.TimeMark),
               hour = SqlFunction.DatePart("hour", m.TimeMark),
               minute = SqlFunctions.DatePart("minute", m.TimeMark)
           })
           .Select(g => new {
               g.Key.dayofWeek, 
               g.Key.hour, 
               g.Key.minute,
               total = g.Average(x => x.Total)
           });
于 2013-05-02T12:09:20.263 回答