没有字典的有序分组

时间:2014-08-14 13:47:40

标签: c# linq

我根据他们服务的煤矿对火车旅行进行分组,然后从人口最多的每个小组开始处理。

List<List<Trip>> tripsByMine = trips.GroupBy(x => x.demand.Mine)
                                    .ToDictionary(x => x.Key, x => x.ToList())
                                    .Values.OrderByDescending(x => x.Count)
                                    .ToList();

在我看来ToDictionary电话是多余的,因为我只想要Values。是否有更短的方法来获得相同的结果?

2 个答案:

答案 0 :(得分:2)

试试这个

List<List<Trip>> tripsByMine2 = trips.GroupBy(x => x.demand.Mine)
    .Select(x => x.ToList())
     .OrderByDescending(x => x.Count)
     .ToList();

答案 1 :(得分:1)

可能的解决方案:

    List<List<Trip>> tripsByMine = trips.GroupBy(x => x.demand.Mine)
                                        .Select(x => new {Key=x.Key, Values=x,Count=x.Count()})
                                        .OrderByDescending(x => x.Count)
.Select(x=>x.Values.ToList())
                                        .ToList();