2010-03-31 31 views
14

這是一個我的工作:Linq的順序由聚集在選擇{}

var fStep = 
      from insp in sq.Inspections 
      where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
       && insp.Model == "EP" && insp.TestResults != "P" 
      group insp by new { insp.TestResults, insp.FailStep } into grp 

      select new 
      { 
       FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
       CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
       grp.Key.TestResults, 
       grp.Key.FailStep, 
       PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

      } ; 

我想排序依據在選擇投影領域的一個或多個。

回答

16

最簡單的變化可能是使用查詢延續:

var fStep = 
     from insp in sq.Inspections 
     where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
      && insp.Model == "EP" && insp.TestResults != "P" 
     group insp by new { insp.TestResults, insp.FailStep } into grp 
     select new 
     { 
      FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
      CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
      grp.Key.TestResults, 
      grp.Key.FailStep, 
      PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

     } into selection 
     orderby selection.FailedCount, selection.CancelCount 
     select selection; 

這主要是相當於用「讓」,說實話 - 真正的區別是,讓引入了範圍變量,而查詢延續有效地啓動了一個新的範圍變量範圍 - 例如,在into selection之後的位中不能引用grp

值得一提的是,這是正是與使用兩個語句:

var unordered = 
     from insp in sq.Inspections 
     where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
      && insp.Model == "EP" && insp.TestResults != "P" 
     group insp by new { insp.TestResults, insp.FailStep } into grp 
     select new 
     { 
      FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
      CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
      grp.Key.TestResults, 
      grp.Key.FailStep, 
      PercentFailed = Convert.ToDecimal(1.0 * grp.Count() /tcount*100) 

     }; 

var fStep = from selection in unordered 
      orderby selection.FailedCount, selection.CancelCount 
      select selection; 
+0

工程蠻好 – 2010-03-31 20:52:24

+0

這是非常好的。好,喬恩 – 2012-08-13 17:19:00

4

包住整個查詢括號和

.OrderBy(x => x.FailedCount).ThenBy(x => x.CancelCount); 
+1

第二'OrderBy'應該是'ThenBy'。 – 2010-03-31 20:36:23

+0

請舉例 – 2010-03-31 20:52:54

+0

非常簡單。謝謝大衛。 – mack 2013-06-17 12:40:18

1

您可以選擇值移動到一個讓分配,並通過之後構建一個訂單。

var fStep = 
    from insp in sq.Inspections 
    where insp.TestTimeStamp > dStartTime && insp.TestTimeStamp < dEndTime 
     && insp.Model == "EP" && insp.TestResults != "P" 
    group insp by new { insp.TestResults, insp.FailStep } into grp 
    let newInsp = new 
    { 
     FailedCount = (grp.Key.TestResults == "F" ? grp.Count() : 0), 
     CancelCount = (grp.Key.TestResults == "C" ? grp.Count() : 0), 
     grp.Key.TestResults, 
     grp.Key.FailStep, 
     PercentFailed = Convert.ToDecimal(1.0 * grp.Count()/tcount * 100) 

    } 
    orderby newInsp.FailedCount, newInsp.CancelCount 
    // or this ... 
    //orderby newInsp.FailedCount 
    //orderby newInsp.CancelCount 
    select newInsp; 
; 
+0

執行時生成錯誤: 無法按類型'<> f__AnonymousType6'2 [System.Int32,System.Int32]'進行排序。 – 2010-03-31 20:51:22

+0

刪除'new ...'並將其替換爲'orderby newInsp.FailedCount,newInsp.CancelCount' – 2010-03-31 23:40:14

+0

謝謝Thomas ...從未嘗試過運行它(只有一個模擬數據模型以確保它至少已編譯。) – 2010-04-01 13:34:23