2011-06-11 71 views
2

我有一個氣象站的數據記錄表,從中查詢結果到WPF ListBoxLINQ通過相應的時間字段查找最大/最小值

表結構是:

  • 日期
  • 時間
  • 臨時
  • rain_today
  • 溼度

我有工作正常的查詢:

var q = from c in db.Apr11log 
       group c by c.LogDate into g 
       orderby g.Key 
       select new 
       { 
        LogDate = g.Key, 
        MaxTemp = g.Max(c => c.Temp), 
        MinTemp = g.Min(c => c.Temp),            
        Rain = g.Max(c => c.Rain_today), 
       };  

不過,我想獲得的最高溫度和最低溫度的相應時間,即

TimeMax = ..... 
TimeMin = ..... 

我GOOGLE和GOOGLE上搜索,但發現沒有什麼用處。

回答

0
let maxTemp = c.Max(c=>c.Temp) 
let minTemp = c.Min(c=>c.Temp) 
select new { 
    LogDate = g.Key, 
    MaxTemp = maxTemp, 
    MinTemp = minTemp, 
    MaxTime = g.FirstOrDefault(c=>c.Temp = maxTemp).Time, 
    MinTime = g.FirstOrDefault(c => c.Temp = minTemp).Time, 
    Rain = g.Max(c => c.Rain_today), 
     };  
+0

感謝您的回覆傢伙。然而,我無法讓他們中的任何一個人工作。儘管如此,我認爲我沒有說清楚。我在每日最大和最小溫度的相應LogTime(同一行)之後,這是通過LogDate通過Group獲得的。 – Corretto 2011-06-11 04:18:47

5
var q = from c in db.Apr11log 
     group c by c.LogDate into g 
     orderby g.Key 
     select new 
     { 
      LogDate = g.Key, 
      MaxRow = g.OrderByDescending(c => c.Temp).Select(c => new { c.LogDate, c.Temp }).First(), 
      MinRow = g.OrderBy(c => c.Temp).Select(c => new { c.LogDate, c.Temp }).First(), 
      Rain = g.Max(c => c.Rain_today), 
     }; 

var q = from c in db.Apr11log 
     group c by c.LogDate into g 
     orderby g.Key 
     let maxRow = g.OrderByDescending(c => c.Temp).First() 
     let minRow = g.OrderBy(c => c.Temp).First() 
     select new 
     { 
      LogDate = g.Key, 
      MaxTemp = maxRow.Temp, 
      MaxTempDate = maxRow.LogDate, 
      MinTemp = minRow.Temp, 
      MinTempDate = minRow.LogDate, 
      Rain = g.Max(c => c.Rain_today), 
     }; 
+0

我是第二個選項的風扇。閱讀更邏輯 – 2011-06-11 03:24:18

+0

感謝這個亞歷克斯,這工作得很好。只需要將LogDate更改爲LogTime :-) – Corretto 2011-06-11 04:41:13