【发布时间】:2021-08-19 13:08:10
【问题描述】:
我正在尝试从我的数据表中选择数据以记录每天 21:00 到 05:00 的记录,我真的很挣扎我卡住了,谁能帮我解决这个问题?
我的数据表
private DataTable mdata = new DataTable();
mdata.Columns.Add("LogDate", typeof(string));
mdata.Columns.Add("Readings", typeof(string));
mdata.Columns.Add("Consumption", typeof(string));
mdata 表中的数据来自我的 sql 选择。
2021-05-29 00:00:00.0000000 2063561 3.39999999999418
2021-05-29 01:00:00.0000000 2063592 3.10000000000582
2021-05-29 02:00:00.0000000 2063622 3
2021-05-29 03:00:00.0000000 2063650 2.79999999998836
2021-05-29 04:00:00.0000000 2063686 3.60000000000582
2021-05-29 05:00:00.0000000 2063741 5.5
2021-05-29 06:00:00.0000000 2063844 10.3000000000175
2021-05-29 07:00:00.0000000 2063935 9.09999999997672
2021-05-29 08:00:00.0000000 2064018 8.30000000001746
2021-05-29 09:00:00.0000000 2064128 11
2021-05-29 10:00:00.0000000 2064220 9.19999999998254
2021-05-29 11:00:00.0000000 2064279 5.90000000002328
2021-05-29 12:00:00.0000000 2064363 8.39999999999418
2021-05-29 13:00:00.0000000 2064439 7.60000000000582
2021-05-29 14:00:00.0000000 2064506 6.69999999998254
2021-05-29 15:00:00.0000000 2064567 6.10000000000582
2021-05-29 16:00:00.0000000 2064633 6.60000000000582
2021-05-29 17:00:00.0000000 2064707 7.39999999999418
2021-05-29 18:00:00.0000000 2064777 7
2021-05-29 19:00:00.0000000 2064856 7.89999999999418
2021-05-29 20:00:00.0000000 2064916 6
2021-05-29 21:00:00.0000000 2064969 5.30000000001746
2021-05-29 22:00:00.0000000 2065005 3.59999999997672
2021-05-29 23:00:00.0000000 2065033 2.80000000001746
2021-05-30 00:00:00.0000000 2065057 2.39999999999418
2021-05-30 01:00:00.0000000 2065083 2.60000000000582
2021-05-30 02:00:00.0000000 2065108 2.5
2021-05-30 03:00:00.0000000 2065135 2.69999999998254
2021-05-30 04:00:00.0000000 2065193 5.80000000001746
2021-05-30 05:00:00.0000000 2065241 4.79999999998836
2021-05-30 06:00:00.0000000 2065320 7.89999999999418
2021-05-30 07:00:00.0000000 2065418 9.80000000001746
2021-05-30 08:00:00.0000000 2065497 7.89999999999418
2021-05-30 09:00:00.0000000 2065600 10.2999999999884
2021-05-30 10:00:00.0000000 2065691 9.10000000000582
2021-05-30 11:00:00.0000000 2065804 11.3000000000175
2021-05-30 12:00:00.0000000 2065904 10
2021-05-30 13:00:00.0000000 2065986 8.19999999998254
2021-05-30 14:00:00.0000000 2066097 11.1000000000058
2021-05-30 15:00:00.0000000 2066197 10
2021-05-30 16:00:00.0000000 2066285 8.79999999998836
2021-05-30 17:00:00.0000000 2066362 7.70000000001164
2021-05-30 18:00:00.0000000 2066454 9.20000000001164
2021-05-30 19:00:00.0000000 2066532 7.79999999998836
2021-05-30 20:00:00.0000000 2066594 6.20000000001164
2021-05-30 21:00:00.0000000 2066654 6
2021-05-30 22:00:00.0000000 2066710 5.59999999997672
2021-05-30 23:00:00.0000000 2066748 3.80000000001746
2021-05-31 00:00:00.0000000 2066777 2.89999999999418
2021-05-31 01:00:00.0000000 2066809 3.20000000001164
2021-05-31 02:00:00.0000000 2066842 3.29999999998836
2021-05-31 03:00:00.0000000 2066886 4.39999999999418
2021-05-31 04:00:00.0000000 2066958 7.20000000001164
以及我当前的选择和分组,提供所有记录
DataTable NightResult = mdata.AsEnumerable()
.Select(x => new
{
Logdate = Convert.ToDateTime(x["LogDate"]).ToString("yyyy/MM/dd"),
Consumption = Convert.ToDouble(x["Consumption"]),
Readings = Convert.ToDouble(x["Readings"])
}).Where(x => x)??? // I am stuck here please help me!!
.GroupBy(x => new { x.Logdate })
.Select(g =>
{
var r = nightdata.NewRow();
r["Day"] = g.First().Logdate;
r["Max"] = g.Max(c => c.Consumption);
r["Min"] = g.Min(d => d.Consumption);
r["Avg"] = g.Average(e => e.Consumption);
return r;
})
.CopyToDataTable();
我只想要多天每天 21:00 到 05:00 之间的记录,但我不知道如何在选择中应用“位置”。
下面是我在上面代码中应用分组之前尝试得到的结果(最好不要在sql中做)
2021-05-29 00:00:00.0000000 2063561 3.39999999999418
2021-05-29 01:00:00.0000000 2063592 3.10000000000582
2021-05-29 02:00:00.0000000 2063622 3
2021-05-29 03:00:00.0000000 2063650 2.79999999998836
2021-05-29 04:00:00.0000000 2063686 3.60000000000582
2021-05-29 05:00:00.0000000 2063741 5.5
2021-05-29 21:00:00.0000000 2064969 5.30000000001746
2021-05-29 22:00:00.0000000 2065005 3.59999999997672
2021-05-29 23:00:00.0000000 2065033 2.80000000001746
2021-05-30 00:00:00.0000000 2065057 2.39999999999418
2021-05-30 01:00:00.0000000 2065083 2.60000000000582
2021-05-30 02:00:00.0000000 2065108 2.5
2021-05-30 03:00:00.0000000 2065135 2.69999999998254
2021-05-30 04:00:00.0000000 2065193 5.80000000001746
2021-05-30 05:00:00.0000000 2065241 4.79999999998836
2021-05-30 21:00:00.0000000 2066654 6
2021-05-30 22:00:00.0000000 2066710 5.59999999997672
2021-05-30 23:00:00.0000000 2066748 3.80000000001746
2021-05-31 00:00:00.0000000 2066777 2.89999999999418
2021-05-31 01:00:00.0000000 2066809 3.20000000001164
2021-05-31 02:00:00.0000000 2066842 3.29999999998836
2021-05-31 03:00:00.0000000 2066886 4.39999999999418
2021-05-31 04:00:00.0000000 2066958 7.20000000001164
我已经尝试过谷歌,并尝试从 stackoverflow 上的一堆帖子中找出它,但我发现是空的
【问题讨论】:
-
.Where(x=> x.Logdate.Hour = 21)
-
嗨,Kamiky,感谢您对 where 部分的帮助,当我尝试它时,我收到以下错误“字符串不包含“小时”的定义,我将尝试将列格式更改为datetime 希望它能修复错误
-
谢谢你它在更改列以键入日期时间后工作现在数据正确我的分组不起作用所以按天分组不再起作用希望根据我的数据分组当天的记录
-
如果现在是 DateTime,您可以改为按 Logdate.Date 分组。
-
T.Schwarz 感谢您的快速帮助,它现在 100% 正常工作