使用linq在组内排序

本文关键字:内排序 linq 使用 | 更新日期: 2023-09-27 17:50:15

我有一个对象列表。我想按日期订购,然后按TransParentType订购,然后再按TransType订购。然后我想按TransParentType分组,在每个分组中,将一个特定的项目(如果存在(放在分组的底部(撤回:特定(。

样本数据:

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)... 

使用linq在组内排序

好吧,从您的预期排序数据来看,我不认为需要分组。

我会做

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);

我相信这样的东西会起作用:

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

您在问题中显示的输出根本不分组,只是:

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<>>(。