我根据他们服务的煤矿把火车班次分组,然后从人口最多的人开始处理每一群人。
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
。有更短的方法来获得同样的结果吗?
发布于 2014-08-14 13:56:05
尝尝这个
List<List<Trip>> tripsByMine2 = trips.GroupBy(x => x.demand.Mine)
.Select(x => x.ToList())
.OrderByDescending(x => x.Count)
.ToList();
发布于 2014-08-14 13:52:46
可能的解决办法:
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();
https://stackoverflow.com/questions/25309642
复制相似问题