2015-05-20 24 views
1

我有一個對象列表。我想按日期排序,然後按TransParentType然後按TransType排序。然後我想通過TransParentType進行分組,並在每個組內放置一個特定項目(如果存在)在組的底部(撤銷:特定)。用linq排序組內

樣本數據:

Date   TransParentType TransType 

2015/05/20 Purchase  investment 
2015/05/20 Redemption withdrawal: b 
2015/05/20 Redemption zz 
2015/05/20 Redemption withdrawal: a 
2015/05/20 Redemption withdrawal: specific 
2015/05/20 Redemption withdrawal: c 
2015/05/14 Purchase  investment 

預期排序的數據:

Date   TransParentType TransType 

2015/05/14 Purchase  investment 
2015/05/20 Purchase  investment 
2015/05/20 Redemption withdrawal: a 
2015/05/20 Redemption withdrawal: b 
2015/05/20 Redemption withdrawal: c 
2015/05/20 Redemption withdrawal: specific 
2015/05/20 Redemption zz 

我試圖做這樣的事情,但沒有成功。 GroupBy不維護我的排序數據。這就像我已經得到。不知道是否有一種方法,以具體項目移動到組的底部,或者如果我必須做手工......

results = results.OrderBy(r => r.Date).ThenBy(r=>r.TransParentType) 
          .ThenBy(r => r.TransType).ToList(); 

var grouped = results.GroupBy(g => g.TransParentType)... 
+0

這是不是真的進行分組的工作。當您需要計算某些記錄組的總計值(總數,總計數等)時,使用GroupBy。你真正在這裏做的是整理。 – jeremcc

回答

1

好了,我沒有看到該組由的需要您的預期分類數據。

我會做

results = results.OrderBy(r => r.Date) 
       .ThenBy(r=>r.TransParentType) 
       //just add an order criterion, checking if TransType == the value that you want at the end 
       //as order by a boolean returns false results first, this will put "widthdrawal: specific" at the end 
       //this will only make a difference for the elements starting with "withdrawal:" 
       .ThenBy(r => r.TransType == "widthdrawal: specific") 
       //finally, order TransType for all elements 
       .ThenBy(r => r.TransType) 
       .ToList(); 

編輯:

按照給定的新規格,我看到的東西(醜陋的),這樣的

 results = results 
        //order by date 
        .OrderBy(m => m.Date) 
        //order by transParentType 
        .ThenBy(m => m.TransParentType) 
        //order by the beginning of TransType (the part which may contain "withdrawal:" 
        .ThenBy(m => m.TransType.Substring(0, Math.Min(11, m.TransType.Length))) 
        .ThenBy(m => m.TransType == "withdrawal: specific") 
        .ThenBy(m => m.TransType); 
+0

這個答案比我的要好一些,因爲正如你指出的那樣,錯誤在排序之前就會出現。尼斯。評論也很好。 ;-) – jeremcc

+0

@jeremcc好,可能稍微有點高性能,但我會說你的第一眼看起來更具可讀性(按布爾的順序並不是絕對明顯的) –

+0

@RaphaëlAlthaus謝謝!這樣可行。很好的解釋 –

0

你在你的問題顯示輸出沒有按」 t組,完全是:

results.OrderBy(r => r.TransParentType).ThenBy(r => r.Date).ThenBy(r => r.TransType == "withdrawal: specific").ThenBy(r => r.TransType); 

如果我嘗試你的代碼,我發現GroupBy確實保持順序,除了明顯地將它分成兩個單獨的組。

如果GroupBy確實打亂順序(雖然我不能看到),那麼你可以使用:

var groupedResults = results.GroupBy(r => r.Trans‎ParentType).Select(grp => grp.OrderBy(r => r.Date).ThenBy(r => r.TransType == "withdrawal: specific").ThenBy(r => r.TransType)); 

,這將給你一個IEnumerable<OrderedEnumerable<>>,這樣你就可以foreach通過每個塊,然後通過foreach那些按順序,等等。 (或者如果針對不同的linq源完成IQueryable<IOrderedQueryable<>>)。

1

我相信這樣的事情會工作:

results = results 
    .OrderBy(r => r.Date) 
    .ThenBy(r => r.TransParentType) 
    .ThenBy(r => r.TransType == "withdrawal: specific" ? 1 : 0) 
    .ThenBy(r => r.TransType).ToList(); 
+0

爲什麼你要''撤銷:特定'之後像'未指定交易'等? –

+1

@JonHanna,因爲這是不可知的問題? –

+1

你是對的,我錯過了關於分組的一點。 –