我有一個對象列表。我想按日期排序,然後按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)...
這是不是真的進行分組的工作。當您需要計算某些記錄組的總計值(總數,總計數等)時,使用GroupBy。你真正在這裏做的是整理。 – jeremcc