【发布时间】:2018-08-11 05:04:16
【问题描述】:
I have been sorting a collection like this. i was wondering whether it can be optimized or not. please have a look:
//ordering elements i.e latest ToDate will be on top and if ToDate are same then latest FromDate will be in top
var posistions = (someModel.Positions.OrderByDescending(x => x.ToDate)
.ThenByDescending(x => x.FromDate).Where(x => !x.ToDate.HasValue).ToArray())
.Concat(someModel.Positions.OrderByDescending(x => x.FromDate)
.ThenByDescending(x => x.ToDate).Where(x => x.ToDate.HasValue).ToArray());
someModel.Positions = posistions.ToArray();
【问题讨论】:
-
为什么要连接列表?也就是说,如果 Todate 没有值,那么它们应该排在最后,但是按 fromDate 排序?
-
首先我想,因为我是通过descend来订购ToDate,如果没有ToDate,它将移动到concat之后写入的块。你猜对了
-
第一个优化是不要将 10 个 LINQ 命令链接在一起。