使用linq to sql

时间:2019-03-22 05:25:53

标签: c# linq linq-to-sql

我有想要这样排序的列表

public class Refund {
    public int RefundStatus { get; set; }
    public DateTime SumbitTime { get; set; }
}

首先按RefundStatus排序,然后:

如果RefundStatus == 1然后以SumbitTime递增,

如果RefundStatus != 1,然后SumbitTime降序。

我应该如何通过linq to sql?

更新:  我跟随Michal Turczyn进行了更改。但是日志输出显示无法翻译

  

Microsoft.EntityFrameworkCore.Query:警告:LINQ表达式   'orderby [p] .RefundStatus asc,([[p] .SumbitTime.Ticks *   Convert(IIF(([[p] .RefundStatus == 1),1,-1),Int64))asc,   EF.Property(?[p] ?,“ Id”)asc'无法翻译,将被翻译为   本地评估。 Microsoft.EntityFrameworkCore.Query:警告:   LINQ表达式'orderby [p] .RefundStatus asc,([p] .SumbitTime.Ticks *   Convert(IIF(([[p] .RefundStatus == 1),1,-1),Int64))asc,   EF.Property(?[p] ?,“ Id”)asc'无法翻译,将被翻译为   本地评估。

3 个答案:

答案 0 :(得分:1)

我会通过以下方式做到这一点:

List<Refund> refundList = new List<Refund>();
// populate your list
refundList = refundList
    // here you could also use:
    //.OrderBy(r => r.RefundStatus)
    // but I don't know if you want it this way
    .OrderBy(r => (r.RefundStatus == 1 ? 1 : -1))
    .ThenBy(r => r.SubmitTime.Ticks * (r.RefundStatus == 1 ? 1 : -1))
    .ToList();

这个想法是,当您将-1中的滴答数乘以DateTime时,它将以降序排列,而不必将您的收藏分为两部分。

答案 1 :(得分:0)

如果我正确理解了您的问题,这应该可以满足您的需求。

var sorted = refundList.GroupBy(refund => refund.RefundStatus)
    .SelectMany(
        group => group.Key == 1 
        ? group.OrderBy(p => p.SumbitTime)
        : group.OrderByDescending(p => p.SumbitTime))
    .ToList();

答案 2 :(得分:0)

您可以使用以下内容。

var sorted = list.GroupBy(refund => refund.RefundStatus)
                 .OrderBy(x=>x.Key)
                 .SelectMany(
                             group => group.Key == 1 ? 
                             group.OrderBy(p => p.SubmitTime)
                             : group.OrderByDescending(p => p.SubmitTime));

完整示例。

        var random = new Random();
        var list = Enumerable.Range(1, 10)
            .Select(x => 
                new Refund
                {
                    RefundStatus = random.Next(0,2),
                    SubmitTime = DateTime.Now.AddMinutes(x)
                });

其中退款定义为

public class Refund
{
    public int RefundStatus { get; set; }
    public DateTime SubmitTime { get; set; }
}

示例输出

**排序前

Refund = 0 - SubmitTime = 22-03-2019 14:22:16
Refund = 1 - SubmitTime = 22-03-2019 14:23:16
Refund = 0 - SubmitTime = 22-03-2019 14:24:16
Refund = 0 - SubmitTime = 22-03-2019 14:25:16
Refund = 0 - SubmitTime = 22-03-2019 14:26:16
Refund = 1 - SubmitTime = 22-03-2019 14:27:16
Refund = 0 - SubmitTime = 22-03-2019 14:28:16
Refund = 1 - SubmitTime = 22-03-2019 14:29:16
Refund = 0 - SubmitTime = 22-03-2019 14:30:16
Refund = 1 - SubmitTime = 22-03-2019 14:31:16

排序后

Refund = 0 - SubmitTime = 22-03-2019 14:31:16
Refund = 0 - SubmitTime = 22-03-2019 14:29:16
Refund = 0 - SubmitTime = 22-03-2019 14:28:16
Refund = 0 - SubmitTime = 22-03-2019 14:26:16
Refund = 0 - SubmitTime = 22-03-2019 14:23:16
Refund = 1 - SubmitTime = 22-03-2019 14:22:16
Refund = 1 - SubmitTime = 22-03-2019 14:24:16
Refund = 1 - SubmitTime = 22-03-2019 14:25:16
Refund = 1 - SubmitTime = 22-03-2019 14:27:16
Refund = 1 - SubmitTime = 22-03-2019 14:30:16