1 回答
TA贡献1840条经验 获得超5个赞
您在询问连接,但您似乎使用它,所以我不太确定您需要什么,但是
如果您希望变量中的 24 条记录按小时分组,您只需按数据的小时运行分组即可。像这样
var groupedbyhour = entity.M_FC66294.GroupBy(entry => entry.epochasdate.Date.AddHours(entry.epochasdate.Hour))//group by hour
.Select(HourGroup => new
{
TankTemp = HourGroup.Average(x => x.Tanktemperature_C),
Bar = HourGroup.Average(x => x.Pressure_bar),
MinOdo = HourGroup.Min(x => x.Odometer_km),
MaxOdo = HourGroup.Max(x => x.Odometer_km),
epochasdate = HourGroup.Max(x => x.epochasdate.Date.AddHours(x.epochasdate.Hour))
}).ToList();
请记住,纪元现在将是几小时组,因此例如 2019/07/30 - 7:00 和 2019/07/30 - 8:00
更新
这是我的完整测试,以便您可以复制。
public class entry
{
public int Odometer_km;
public DateTime epochasdate;
public int Tanktemperature_C;
public int Pressure_bar;
}
class Program
{
static void Main(string[] args)
{
List<entry> entries = new List<entry>(){
new entry() {epochasdate = new DateTime(2019, 07, 29, 7, 2, 1), Odometer_km = 10, Pressure_bar = 1, Tanktemperature_C = 23},
new entry() { epochasdate = new DateTime(2019, 07, 29, 7, 1, 1), Odometer_km = 20, Pressure_bar = 2, Tanktemperature_C = 25 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 7, 3, 1), Odometer_km = 22, Pressure_bar = 2, Tanktemperature_C = 24 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 7, 22, 1), Odometer_km = 25, Pressure_bar = 4, Tanktemperature_C = 22 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 8, 24, 1), Odometer_km = 36, Pressure_bar = 2, Tanktemperature_C = 20 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 8, 21, 1), Odometer_km = 42, Pressure_bar = 3, Tanktemperature_C = 19 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 8, 29, 1), Odometer_km = 50, Pressure_bar = 2, Tanktemperature_C = 21 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 8, 22, 1), Odometer_km = 55, Pressure_bar = 4, Tanktemperature_C = 20 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 8, 52, 1), Odometer_km = 62, Pressure_bar = 2, Tanktemperature_C = 19 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 9, 43, 1), Odometer_km = 80, Pressure_bar = 3, Tanktemperature_C = 17 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 9, 22, 1), Odometer_km = 120, Pressure_bar = 1, Tanktemperature_C = 18 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 9, 12, 1), Odometer_km = 140, Pressure_bar = 3, Tanktemperature_C = 16 },
new entry() { epochasdate = new DateTime(2019, 07, 29, 9, 31, 1), Odometer_km = 156, Pressure_bar = 2, Tanktemperature_C = 17 }
};
var groupedbyhour = entries.GroupBy(entry => entry.epochasdate.Date.AddHours(entry.epochasdate.Hour))//group by hour
.Select(HourGroup => new
{
TankTemp = HourGroup.Average(x => x.Tanktemperature_C),
Bar = HourGroup.Average(x => x.Pressure_bar),
MinOdo = HourGroup.Min(x => x.Odometer_km),
MaxOdo = HourGroup.Max(x => x.Odometer_km),
MeasureTime = HourGroup.Max(x => x.epochasdate.Date.AddHours(x.epochasdate.Hour))
}).ToList();
}
}
如果您的 epochasdate 类型不是日期时间并且由于某种原因无法分组,请使用 .addhour 并向值添加 0 小时,它应该返回您的 groupby 功能可以理解的 DateTime 结果
- 1 回答
- 0 关注
- 103 浏览
添加回答
举报