使用 linq 在组内排序

Sort within group with linq

我有一个对象列表。我想按日期排序,然后按 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)... 

好吧,我从你的预期排序数据中看不到分组依据的需要。

我愿意

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.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 再次按顺序通过那些,依此类推。 (或者 IQueryable<IOrderedQueryable<>> 如果针对不同的 linq 源完成)。

我相信这样的事情会奏效:

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