C# linq中按时间顺序的日期时间排序

C# linq中按时间顺序的日期时间排序,c#,.net,asp.net-mvc,linq,C#,.net,Asp.net Mvc,Linq,下面是我的代码,我想按时间顺序查看约会,如降序,但按时间升序: 无法做到这一点,有人能帮我吗 列出约会=\u diagnosticRepo.GetFacilityAppointsReportVersion(FacilityID)。其中(x=>x.Status!=约会重新安排eId) .ToList() 我希望看到约会按时间顺序排列,如降序,但按时间升序,约会日期是日期变量,时间是时间变量,那么我如何实现这一点 例如,如果一个约会是在同一个日期和不同的时间进行的,那么对于约会顺序,时间应该是升序的

下面是我的代码,我想按时间顺序查看约会,如降序,但按时间升序:

无法做到这一点,有人能帮我吗

列出约会=\u diagnosticRepo.GetFacilityAppointsReportVersion(FacilityID)。其中(x=>x.Status!=约会重新安排eId) .ToList()

我希望看到约会按时间顺序排列,如降序,但按时间升序,约会日期是日期变量,时间是时间变量,那么我如何实现这一点

例如,如果一个约会是在同一个日期和不同的时间进行的,那么对于约会顺序,时间应该是升序的

*预约1 06-07-2019上午10:00 *预约2 06-07-2019上午11:00 *约会时间:2019年7月5日下午10:00

            DateTime[] dates = new  DateTime[] { 
                DateTime.Parse("06-07-2019 10:00AM"),
                DateTime.Parse("06-07-2019 11:00AM"),
                DateTime.Parse("05-07-2019 10:00 pm")
            };

            DateTime[] orderDates = dates
                .OrderByDescending(x => x.Date)
                .GroupBy(x => x.Date)
                .SelectMany(x => x.OrderBy(y => y.TimeOfDay))
                .ToArray();

这也应该起作用

            DateTime[] orderDates2 = dates
                .OrderByDescending(x => x.Date)
                .ThenBy(x => x.TimeOfDay)
                .ToArray();

我刚刚在dotnetfiddle中运行了这个,它看起来很好:

    DateTime[] dates = new  DateTime[] { 
       DateTime.Parse("06-07-2019 10:00AM"),
       DateTime.Parse("06-07-2019 11:00AM"),
       DateTime.Parse("05-07-2019 10:00 pm")
    };

    foreach(var d in dates.OrderBy(x => x.Date).ThenBy(x => x.TimeOfDay))
    {
        Console.WriteLine(d.ToString("dd MMM yyyy hh:mm"));
    }
它产生以下输出:

2019年5月7日10:00

2019年6月7日10:00


2019年6月7日11:00

您必须首先确保您订购的是日期时间,而不是字符串。在这种情况下,字符串排序不正确

List<DiagnosticPatientAppointmentModel> appointments = _diagnosticRepo
    .GetFacilityAppointmentsReportVersion(FacilityID)
    .Where(x => x.Status != AppointmentRescheduledID)
    .ToList()
    .OrderByDescending(a => DateTime.Parse(a.ScheduledTime).Date)
    .ThenBy(a => DateTime.Parse(a.ScheduledTime).Time);
List约会=\u diagnosticRepo
.GetFacilityAppointsReportVersion(FacilityID)
.Where(x=>x.Status!=任命重新安排EDID)
托利斯先生()
.OrderByDescending(a=>DateTime.Parse(a.ScheduledTime.Date)
.ThenBy(a=>DateTime.Parse(a.ScheduledTime.Time);
以下是一个工作示例:

new List<dynamic> {
    new { ScheduledTime = "6/7/2019 11:45 PM" },
    new { ScheduledTime = "6/7/2019 10:45 PM" },
    new { ScheduledTime = "6/6/2019 9:45 PM" }
}
.OrderByDescending(a => DateTime.Parse(a.ScheduledTime).Date)
.ThenBy(a => DateTime.Parse(a.ScheduledTime).TimeOfDay);
新列表{
新的{ScheduledTime=“6/7/2019 11:45 PM”},
新的{ScheduledTime=“6/7/2019 10:45 PM”},
新的{ScheduledTime=“6/6/2019 9:45 PM”}
}
.OrderByDescending(a=>DateTime.Parse(a.ScheduledTime.Date)
.ThenBy(a=>DateTime.Parse(a.ScheduledTime.TimeOfDay);

您描述的不是降序。上午6时10分,上午6时11分(上升),下午5时10分(下降)。您只能按实际顺序进行排序,因此6-11、6-10、5-10或5-10、6-10、6-11您要求的不是时间顺序。这是基于同一字段的两个不同订单。在SQL中,您必须分别提取日期和时间部分以及顺序,即按cast(appointmentDT as date)desc、cast(appointmentDT as time)asc提取日期和时间部分以及顺序(x=>x.AppointmentDateTime.TimeOfDay)可能?如果您想要不同的订单方向,您可能应该分别存储日期和时间并对它们进行索引。这也会使按日期筛选更快,解释代码的回答比简单地说“试试这个”的回答更少被否决还有代码。你为什么要对条目进行分组?OP希望每天的日期减少,时间增加。Groupby是唯一的方法。如果你不想得到更好的答案,就不要给出否定的分数。no Groupby不是唯一的方法,它只是通过列表约会来工作=\u diagnosticRepo.GetFacilityAppointsReportVersion(FacilityID)。在哪里(x=>x.Status!=AppointmentRescheduledID).ToList().OrderbyDescending(x=>x.Date)。然后是By(x=>x.Time);注意OPs要求-第一次订购应为OrderbyDescending for Date