LINQ OrderByDescdending for date field is not working razor pages

Blooming Developer 281 Reputation points
2023-02-03T06:59:51.21+00:00

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
{count} votes

3 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,926 Reputation points Volunteer Moderator
    2023-02-05T22:45:55.7766667+00:00

    as you don't show any code, hard to guess what you did wrong.

    0 comments No comments

  2. 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.


  3. 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

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.