1

我有一个名为“VideoData”的后端表,其中包含以下形式的数据:

    VideoID       RecordingStarted         RecordingEnded
    ==============================================================
    abc123        2013-03-01 15:30:00      2013-03-01 15:40:00        
    def123        2013-03-06 12:00:00      2013-03-06 12:40:00
    ijk123        2013-03-10 11:00:00      2013-03-10 11:05:00
    klm123        2013-03-12 10:05:00      2013-03-12 10:25:00
    And list goes on .......
    .......................
    .............................

使用实体框架,我想获取例如 2013 年 3 月的视频总小时数,以这样的方式,捕获的视频总小时数为每周。以下列方式举例:

    Mar 1, 2013     Mar 8, 2013      Mar 15, 2013     Mar 22, 2013
   ================================================================
   500              300              350              200

我用谷歌搜索了很多,但无法弄清楚如何准确地做到这一点。请指导。谢谢你的帮助。

4

2 回答 2

6

我讨厌否决 Ilya 的出色工作(对不起,伙计,至少我支持你),但由于这是实体框架,因此可以使用SqlFunctions让数据库在一个查询中完成这项工作:

context.Videos.Select(t => new
  {
      Year = SqlFunctions.DatePart("yyyy", t.RecordingStarted),
      Week = SqlFunctions.DatePart("ww", t.RecordingStarted),
      Hours = SqlFunctions.DateDiff("hh", t.RecordingStarted, t.RecordingEnded)
  })
.GroupBy(x => new { x.Year, x.Week} )
.Select (x => new { x.Key.Year, x.Key.Week, TotalHours = x.Sum(p => p.Hours)} )

输出将类似于

2013  9 500
2013 10 300
...

在 Sql Server 中,从年 + 周到日期是非常困难的。如果这确实是一个要求,您可以考虑构建一个包含年 + 周数和第一天日期的参考表。或者获取内存中的数据 ( .ToList()) 并使用 C# 转换数据。

于 2013-03-25T19:41:08.200 回答
2

好吧,这个任务似乎很有趣。我已经使用本地数据实现了基本功能。随意评论更多功能。你有点乱

//local data for testing
var data = new[] {
    new {VideoId = "abc123",RecordindStarted = DateTime.Parse("2013-03-01 15:30:00"),RecordingEnded = DateTime.Parse("2013-03-01 15:40:00")},        
    new {VideoId = "def123",RecordindStarted = DateTime.Parse("2013-03-06 12:00:00"),RecordingEnded = DateTime.Parse("2013-03-06 12:40:00")},
    new {VideoId = "de1223",RecordindStarted = DateTime.Parse("2013-03-06 12:30:00"),RecordingEnded = DateTime.Parse("2013-03-06 12:50:00")},
    new {VideoId = "ijk123",RecordindStarted = DateTime.Parse("2013-03-10 11:00:00"),RecordingEnded = DateTime.Parse("2013-03-10 11:05:00")},
    new {VideoId = "klm123",RecordindStarted = DateTime.Parse("2013-03-12 10:05:00"),RecordingEnded = DateTime.Parse("2013-03-12 10:25:00")},
    new {VideoId = "klm123",RecordindStarted = DateTime.Parse("2013-03-12 10:05:00"),RecordingEnded = DateTime.Parse("2013-03-13 10:25:00")},
};


var calendar = new GregorianCalendar();

var ungroupedTotalHours = data.Select(d => GetHoursPerDays(d.RecordindStarted, d.RecordingEnded));

var groupedTotalHours = 
         ungroupedTotalHours.SelectMany(v => v)
                            .GroupBy(v=> v.Key)
                            .ToDictionary(v => v.Key, v => v.Sum(val => val.Value));

var result = 
 groupedTotalHours.GroupBy(v =>calendar.GetWeekOfYear( v.Key, CalendarWeekRule.FirstDay, DayOfWeek.Monday))
                  .ToDictionary(v => "Week "+ v.Key, row => row.Sum(val => val.Value));

Console.WriteLine ( string.Join(Environment.NewLine, result.Select(r => r.Key +" has "+r.Value+" hours")) );

核心逻辑转到此方法:

public IDictionary<DateTime, int> GetHoursPerDays(DateTime start, DateTime end)
{
    if(end.Date == start.Date)
        return new Dictionary<DateTime, int>{{start.Date, (end -  start).Minutes}};
    return Enumerable.Range(1, (int)(end -  start).TotalHours)
                    .Select(v => start.AddHours(v))
                    .GroupBy(v => v.Date)
                    .ToDictionary( v => v.Key, r => r.Count());
}

印刷:

Week 9 has 10 hours
Week 10 has 65 hours
Week 11 has 44 hours
于 2013-03-25T15:35:28.210 回答