locked
Entity Framework: How to use OrderBy with Descending order? RRS feed

  • Question

  • We can use the following query to get the last log record:
    var

     

    nextLog = EntityContext.SystemLog.OrderBy("it.LogID DESC").First();

    if we want to use the following query to get the collection of log with Descending order
    How do we do?

    var

     

    logs = EntityContext.SystemLog.Where(s => (s.LogID >= 150)).OrderBy(c => c.LogID);

    Thx!

    Wednesday, November 11, 2009 10:56 PM

Answers

  • OrderBy sorts in ascending order by default, you want to use ...OrderByDescending(c=>c.LogID).
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, November 11, 2009 11:38 PM
    Moderator

All replies

  • OrderBy sorts in ascending order by default, you want to use ...OrderByDescending(c=>c.LogID).
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, November 11, 2009 11:38 PM
    Moderator
  • Thanks for the update. Is it possible to use the OrderBy to order on more than one colum? For example something equivalent to:

    select * from person ... blah ... order by FirstName asc, LastName asc, DateOfBirth desc

     

    In other words have a secondary, tertiary etc. sort column.

    Monday, October 25, 2010 2:59 PM
  • You can u can use Orderby followed by thenby and you can have as many thenby as u like
    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    Tuesday, October 26, 2010 6:21 AM