对LINQ查询的建议

本文关键字:查询 LINQ | 更新日期: 2023-09-27 18:18:35

我有以下类用于在List中存储数据:

public class Values
    {
        public int ParamId { get; set; }
        public DateTime TimeStamp { get; set; }
        public double Value { get; set; }
    }

我正在做以下事情:

IList<Values> val = new List<Values>();
//12 december 2011 10 hour
Values v1 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 0), Value = 345.0d };
Values v2 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 1), Value = 350.0d };
Values v3 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 2), Value = 355.0d };
Values v4 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 3), Value = 344.0d };
Values g1 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 0), Value = 3000.0d };
Values g2 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 1), Value = 3100.0d };
Values g3 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 2), Value = 3150.0d };
Values g4 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 0, 3), Value = 3125.0d };
Values v5 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 0), Value = 355.0d };
Values v6 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 1), Value = 360.0d };
Values v7 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 2), Value = 365.0d };
Values v8 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 3), Value = 360.0d };
Values g5 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 0), Value = 3150.0d };
Values g6 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 1), Value = 3200.0d };
Values g7 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 2), Value = 3250.0d };
Values g8 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 3, 3), Value = 3225.0d };
//............... up to 57 minutes
Values v9 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 0), Value = 350.0d };
Values v10 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 1), Value = 365.0d };
Values v11 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 2), Value = 367.0d };
Values v12 = new Values { ParamId = 100, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 3), Value = 370.0d };
Values g9 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 0), Value = 3250.0d };
Values g10 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 1), Value = 3240.0d };
Values g11 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 2), Value = 3200.0d };
Values g12 = new Values { ParamId = 101, TimeStamp = new DateTime(2011, 12, 20, 10, 57, 3), Value = 3235.0d };
val.Add(v1); val.Add(v2); val.Add(v3); val.Add(v4);
val.Add(v5); val.Add(v6); val.Add(v7); val.Add(v8);
val.Add(v9); val.Add(v10); val.Add(v11); val.Add(v12);
val.Add(g1); val.Add(g2); val.Add(g3); val.Add(g4);
val.Add(g5); val.Add(g6); val.Add(g7); val.Add(g8);
val.Add(g9); val.Add(g10); val.Add(g11); val.Add(g12);

我想通过计算每个参数的平均值,从这个三分钟的数据中得到每小时的数据。例如时间为00:00,00:03,00:06,…, 00:57,计算参数100和101在00小时内的平均值。实际上,val List中的数据范围可能是几个小时或几天。

我如何才能正确地做到这一点,并与良好的代码?谢谢。

对LINQ查询的建议

像这样:

var valuesByHourQuery = from val in vals
                        group val by val.ParamId into valsByParam
                        select new
                        {
                            ParamId = valsByParam.Key,
                            AvarageValues = from v in valsByParam
                                            let timeStampWithoutMinutes = new DateTime(v.TimeStamp.Year, v.TimeStamp.Month, v.TimeStamp.Day, v.TimeStamp.Hour, 0, 0)
                                            group v by timeStampWithoutMinutes into valsByHours 
                                            select new
                                            {
                                                Hour = valsByHours.Key,
                                                Value = valsByHours.Average(v => v.Value)
                                            }
                        };