我想查询一下,数据按时间分组(SQL time
数据类型,而不是datetime
),间隔为2小时。
例如,结果将是这样的:
+---------------+-------+
| Time Group | Count |
+---------------+-------+
| 00:01 - 02:00 | 3 |
| 02:01 - 04:00 | 5 |
| 04:01 - 06:00 | 8 |
| ... | ... |
| 22:01 - 24:00 | 2 |
+---------------+-------+
请注意,时间组是预定义的。 我设法有这样一个天真的查询:
var temp = from a in myTable select a;
var result0To2 = temp.Where(a => a.activityTime.Hours > 0 && a.activityTime.Hours <= 2).Count();
var result2To4 = temp.Where(a => a.activityTime.Hours > 2 && a.activityTime.Hours <= 4).Count();
var result4To6 = temp.Where(a => a.activityTime.Hours > 4 && a.activityTime.Hours <= 6).Count();
...
...
var result20To24 = temp.Where(a => a.activityTime.Hours > 22 && a.activityTime.Hours <= 24).Count();
我认为应该有效。但是有更好的方法吗?
答案 0 :(得分:1)
这是我运行的测试程序,您可以根据您的具体要求调整代码。它使用LINQ的GroupBy()
谓词,完全基于this answer to a similar question(我将其更改为检查小时而不是分钟)。
public class Temp
{
public DateTime activityTime { get; set; }
public object item_info { get; set; }
}
当然你可以使用自己的课程,我的课程只是为了测试。
List<Temp> items = new List<Temp> //populate with dummy data
{
new Temp { activityTime = DateTime.Parse("2017-12-11 11:43:00"), item_info = "something1" },
new Temp { activityTime = DateTime.Parse("2017-12-11 11:46:00"), item_info = "something2" },
new Temp { activityTime = DateTime.Parse("2017-12-11 11:57:00"), item_info = "something3" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:02:00"), item_info = "something4" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:04:00"), item_info = "something5" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:06:00"), item_info = "something6" },
new Temp { activityTime = DateTime.Parse("2017-12-11 12:58:00"), item_info = "something7" },
new Temp { activityTime = DateTime.Parse("2017-12-11 13:05:00"), item_info = "something8" },
new Temp { activityTime = DateTime.Parse("2017-12-11 13:29:00"), item_info = "something9" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:53:00"), item_info = "something10" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:55:00"), item_info = "something11" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:59:00"), item_info = "something12" },
new Temp { activityTime = DateTime.Parse("2017-12-11 14:59:00"), item_info = 13 },
new Temp { activityTime = DateTime.Parse("2017-12-11 15:26:00"), item_info = 15 }
};
var groups = items.GroupBy(x =>
{
var stamp = x.activityTime;
stamp = stamp.AddMinutes(-(stamp.Minute));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return stamp;
})
.Select(g => new { TimeGroup = g.Key.ToString("h tt") + " - " + g.Key.AddHours(1).ToString("h tt"), Value = g.Count() })
.ToList();
要更改分组,您可以修改GroupBy()
谓词。
TimeGroup
变量可以根据您需要的显示进行更改(日期格式化,或者您可以像在您的情况下一样添加1分钟来“02:01 - 04:00”。
间隔2小时:
var groups = items.GroupBy(x =>
{
var stamp = x.activityTime;
stamp = stamp.AddHours(-(stamp.Hour % 2));
stamp = stamp.AddMinutes(-(stamp.Minute));
stamp = stamp.AddMilliseconds(-stamp.Millisecond - 1000 * stamp.Second);
return stamp;
})
.Select(g => new { TimeGroup = g.Key.ToString("h tt") + " - " + g.Key.AddHours(2).ToString("h tt"), Value = g.Count() })
.ToList();
警告,对于较大的数据集,此操作可能会耗费大量时间。