C# 日期排序优化

C# 日期排序优化,c#,arrays,linq,sorting,C#,Arrays,Linq,Sorting,你可以这样做。我不确定您需要从订单中获得什么样的极性,但它很容易改变 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 w

你可以这样做。我不确定您需要从订单中获得什么样的极性,但它很容易改变

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();
输出

var positions = model.Positions
                     .OrderByDescending(x => x.ToDate.HasValue)
                     .ThenByDescending(x => x.ToDate)
                     .ThenByDescending(x => x.FromDate);

foreach (var pos in positions)
   Console.WriteLine(pos.ToDate+" : " + pos.FromDate);    

你可以这样做。我不确定您需要从订单中获得什么样的极性,但它很容易改变

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();
输出

var positions = model.Positions
                     .OrderByDescending(x => x.ToDate.HasValue)
                     .ThenByDescending(x => x.ToDate)
                     .ThenByDescending(x => x.FromDate);

foreach (var pos in positions)
   Console.WriteLine(pos.ToDate+" : " + pos.FromDate);    
参见小提琴:

对已接受答案的小调整:

8/8/2018 5:34:49 AM : 8/8/2018 5:34:49 AM
8/7/2018 5:34:49 AM : 7/2/2018 5:34:49 AM
8/6/2018 5:34:49 AM : 6/22/2018 5:34:49 AM
null : 8/1/2018 5:34:49 AM
null : 7/22/2018 5:34:49 AM
此代码足以复制结果

使用此排序条件查看结果

.OrderBy(x => x.ToDate ?? DateTime.MinValue)
.ThenByDescending(x => x.FromDate);
参见小提琴:

对已接受答案的小调整:

8/8/2018 5:34:49 AM : 8/8/2018 5:34:49 AM
8/7/2018 5:34:49 AM : 7/2/2018 5:34:49 AM
8/6/2018 5:34:49 AM : 6/22/2018 5:34:49 AM
null : 8/1/2018 5:34:49 AM
null : 7/22/2018 5:34:49 AM
此代码足以复制结果

使用此排序条件查看结果

.OrderBy(x => x.ToDate ?? DateTime.MinValue)
.ThenByDescending(x => x.FromDate);

你为什么要把这些列表连在一起?也就是说,如果Todate没有值,那么它们应该排在最后,不管是按fromDate排序的吗?首先我想,当我按Descent排序Todate时,如果没有Todate,它将移动到concat之后写入的块。您猜对了,第一个优化就是不要将10个LINQ命令链接在一起。为什么要将列表连接起来?也就是说,如果Todate没有值,那么它们应该排在最后,不管是按fromDate排序的吗?首先我想,当我按Descent排序Todate时,如果没有Todate,它将移动到concat之后写入的块。您猜对了,第一个优化就是不要将10个LINQ命令链接在一起。