两个空日期之间的OrderBy之差

时间:2019-05-09 12:17:37

标签: c# asp.net-core entity-framework-core

我有两个可为空的日期,如果其中两个具有值,我想将它们之间的天数定为OrderBy,否则将其放在开头或结尾

这是我目前的想法,可以很好地转化为SQL,但是我该如何改进呢?

public static IQueryable<Order> ApplyOrderBy(this IQueryable<Order> input, Rules rules)
{
    switch (rules.OrderBy)
    {
        case "Date":
        {
            input = rules.Direction == "asc" 
            ? 
            input.OrderBy
            (
                x =>
                (x.SentDate == null ? new DateTime(1990,1,1).Date : x.SentDate.Value.Date)
                -
                (x.ReceivedDate == null ? new DateTime.Now(1990,1,1).Date : x.ReceivedDate.Value.Date)
            )
            :
            input.OrderByDescending
            (
                x =>
                (x.SentDate == null ? new DateTime(2990,1,1).Date : x.SentDate.Value.Date)
                -
                (x.ReceivedDate == null ? new DateTime.Now(1990,1,1).Date : x.ReceivedDate.Value.Date)
            );
            break;
        }
    }
}

0 个答案:

没有答案