2017-02-01 61 views
1

使用lambda我有一個集合,我需要用Linq日期降序(DateProcessed場)來訂購吧,首先我用兩個可能的密鑰分組:BookedEmpty。但它沒有被訂購的數據..的GroupBy,OrderByDescending Linq中

這是我的表達:

IEnumerable<IGrouping<string, MyClassMongo>> sub = Model.MyCollection.GroupBy(f => f.IsBooked ? "Booked" : "Empty").OrderByDescending(f => f.FirstOrDefault().DateProcessed); 

我很困惑,因爲我是第一個分組,我知道分組集合後兩(BookedEmpty被分裂),所以我不知道如何處理排序,因爲我第一次分組

回答

1

如果您要查詢內存中的集合,然後只需將順序分組之前:

IEnumerable<IGrouping<string, MyClassMongo>> sub = 
    Model.MyCollection 
     .OrderByDescending(f => f.DateProcessed) 
     .GroupBy(f => f.IsBooked ? "Booked" : "Empty"); 

每組內的項目將按DateProcessed排序。

0

如果您想先分組,然後對組內的結果進行排序。你可以嘗試一些東西像下面

Model.MyCollection 
    .GroupBy(f => f.IsBooked, 
      (f, g) => new{Key=f.DateProcessed, Group = g.OrderByDescending(c=>c.IsBooked)}) 
    .OrderByDescending(f => f.Key); 

PS:代碼可能已經沒有測試過的語法問題,但這個想法應該是創造的關鍵withing每組因此我們可以通過

訂購