2011-12-20 108 views
0

我有一個列表存儲數據如下類:建議LINQ查詢

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的數據計算00小時參數100和101的平均值。實際上,val列表中的數據範圍可能只有幾小時或幾天。

如何正確執行此操作並使用正確的代碼?謝謝。

+0

請你可以寫半句多一點清楚?真的很難說出你實際想要達到的目標。 – Michael 2011-12-20 12:47:23

回答

3

事情是這樣的:

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) 
              } 
         }; 
+0

對於您的LINQ代碼,10小時內的平均值將爲357.166666666667,但是正確的值爲358.錯誤在哪裏?每小時10小時的值必須包含00:00:00.000和23:59:59.000的範圍。謝謝。 – amaranth 2011-12-21 04:50:47