[英]date time sorting in chronological order in linq
下面是我的代码,我想按时间顺序查看约会,如降序但升序:
无法实现这一点,任何人都可以帮助我
列表约会 = _diagnosticRepo.GetFacilityAppointmentsReportVersion(FacilityID).Where(x => x.Status != AppointmentRescheduledID) .ToList();
我想按时间顺序查看约会,例如降序但时间升序,并且约会日期是日期变量和时间变量,所以我如何实现这一点
例如。 如果在同一日期和不同时间进行约会,则时间应按约会顺序升序排列
*约会 1 06-07-2019 上午 10:00 *约会 2 06-07-2019 上午 11:00 *约会 3 05-07-2019 晚上 10:00
您必须首先确保您订购的东西是 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);
我刚刚在 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
尝试以下:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.