【问题标题】:date time sorting in chronological order in linqlinq中按时间顺序排序的日期时间
【发布时间】:2019-09-06 12:31:04
【问题描述】:

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

无法做到这一点,谁能帮帮我

列出约会 = _diagnosticRepo.GetFacilityAppointmentsReportVersion(FacilityID).Where(x => x.Status != AppointmentRescheduledID) .ToList();

我想按时间顺序查看约会,如降序但升序,约会日期是日期变量,时间是时间变量,那么我该如何实现这一点

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

*约会 1 06-07-2019 10:00Am *预约 2 06-07-2019 上午 11:00 *预约 3 05-07-2019 10:00 pm

【问题讨论】:

  • 你描述的不是降序。 6 @ 10 am,6 @ 11 am(上升),5 @ 10 pm(下降)。您只能通过真实订单订购,因此 6-11、6-10,5-10 或 5-10,6-10,6-11
  • 您要求的不是时间顺序。这是基于同一字段的两个不同订单。在 SQL 中,您必须分别提取日期和时间部分并按每个部分排序,即 ORDER BY cast(appointmentDT as date) desc, cast(appointmentDT as time) asc
  • DateTime.Date 为降序,DateTime.TimeOfDay 为升序
  • .OrderByDescending(x => x.AppointmentDateTime.Date).ThenBy(x => x.AppointmentDateTime.TimeOfDay) 也许?
  • 如果您想要不同的订购方向,您可能应该分别存储日期和时间并将它们编入索引。这也将使按日期过滤更快

标签: c# .net asp.net-mvc linq


【解决方案1】:

您必须首先确保您订购的是 DateTime 而不是字符串。在这种情况下,字符串无法正确排序。

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);

这是一个工作示例:

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);

【讨论】:

    【解决方案2】:

    我刚刚在 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

    【讨论】:

    • 注意 OPs 要求 - 第一次订购应该是 OrderByDescending 的日期
    【解决方案3】:

    尝试以下:

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

    【讨论】:

    • 解释代码的答案比那些简单地说“试试这个”和代码的答案更少。
    • 为什么要对条目进行分组?
    • OP希望每天减少日期和增加时间。 Groupby 是唯一的方法。如果你没有试图得到一个比不给负分更好的答案。
    • no groupby 不是它通过 List 工作的唯一方式约会 = _diagnosticRepo .GetFacilityAppointmentsReportVersion(FacilityID) .Where(x => x.Status != AppointmentRescheduledID) .ToList().OrderbyDescending( x=> x.Date).ThenBy(x=>x.Time);
    猜你喜欢
    • 2012-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    相关资源
    最近更新 更多