gpt4 book ai didi

c# - LINQ:选择表中每分钟的第一个值

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:27 25 4
gpt4 key购买 nike

我有这样一个对象列表:

+-------------+------------+---------------------+
| ID | Value | Snapshot |
+-------------+------------+---------------------+
| 1 | 1 | 2019-03-07 20:00:04 |
| 2 | 2 | 2019-03-07 20:01:15 |
| 3 | 1 | 2019-03-07 20:01:23 |
| 4 | 3 | 2019-03-07 20:01:45 |
| 5 | 5 | 2019-03-07 20:02:10 |
| 6 | 7 | 2019-03-07 20:02:45 |
| 7 | 1 | 2019-03-07 20:03:10 |
| 8 | 0 | 2019-03-07 20:04:30 |
+-------------+------------+---------------------+

如何只选择每分钟或每小时的记录...

结果应该是这样的:

+-------------+------------+---------------------+
| ID | Value | Snapshot |
+-------------+------------+---------------------+
| 1 | 1 | 2019-03-07 20:00:04 |
| 2 | 2 | 2019-03-07 20:01:15 |
| 5 | 5 | 2019-03-07 20:02:10 |
| 7 | 1 | 2019-03-07 20:03:10 |
| 8 | 0 | 2019-03-07 20:04:30 |
+-------------+------------+---------------------+

当我使用 thisq SQL 查询获取这些类型的记录时:

SELECT * FROM log 
WHERE Snapshot IN ( SELECT MIN(Snapshot) AS snaptime FROM log GROUP BY DATE_FORMAT(snaptime,'%Y-%m-%d %H:%i'))

但如果可能的话,我需要以某种方式将此查询转换为 LINQ。

最佳答案

所以你想按分钟分组,只显示每分钟的第一条记录:

var query = list
.OrderBy(x => x.Snapshot)
.GroupBy(x => x.Snapshot.Date.AddMinutes((int)x.Snapshot.TimeOfDay.TotalMinutes))
.Select(minuteGroup => minuteGroup.First());

关于c# - LINQ:选择表中每分钟的第一个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57954388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com