as you don't show any code, hard to guess what you did wrong.
LINQ OrderByDescdending for date field is not working razor pages
Blooming Developer
281
Reputation points
Hi,
i want to display records based on booked dates. For that i used OrderByDescending in LINQ query, but it is not sorting based on date field.
ManageBooking = await _context.ManageBooking.Where(e => (e.BookingDate >= DateTime.Today && e.EmployeeNo == EmployeeNumber)).ToListAsync();
ManageBooking = ManageBooking.OrderByDescending(e => e.BookingDate).ToList();
Any help would be appreciated.
Thanks,
Teena John
Developer technologies | ASP.NET | ASP.NET Core
4,824 questions
3 answers
Sort by: Most helpful
-
Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
2023-02-05T22:45:55.7766667+00:00 -
DOTNETVIC 0 Reputation points
2023-02-06T03:29:43.2266667+00:00 Assuming the list is indeed passed to your view correctly, you can order it on the view.
For example:
@model List<ManageBooking> foreach(var item in Model.OrderByDescending(e => e.BookingDate)) { }
if this works, and this is used by one application only then just remove the order by statement (OrderByDescending(e => e.BookingDate)) from your repo and just order it in your views.
-
Naimish Makwana 175 Reputation points
2023-02-07T09:45:30.1566667+00:00 Hello
Try below code.
ManageBooking = await _context.ManageBooking .Where(e => (e.BookingDate >= DateTime.Today && e.EmployeeNo == EmployeeNumber)) .OrderByDescending(e => e.BookingDate) .ToListAsync();
Thanks
Naimish Makwana