locked
Order by 4 columns query in EF with lambda RRS feed

  • Question

  • User-1223304583 posted

    anyone can post what will be query in EF if i have order by clause for 4 columns.

    i got one with lambda

    var qry = ctx.DestinationTimings.Where(x => x.DestinationID == this.ID)
                     .OrderBy(t => t.Date ?? DateTime.MaxValue)
                     .ThenBy(t => SqlFunctions.CharIndex(t.DayOfWeek + ",", "MON,TUE,WED,THU,FRI,SAT,SUN"))
                     .ThenBy(t => t.Time);

    how to have mutiple order by ASC and DESC because in above query there is no ascending or descending word.

    how to do the same without lambda with ASC and DESC keyword.

    please post a sample query with lambda.

    Wednesday, February 8, 2017 11:59 AM

Answers

  • User1771544211 posted

    Hi mamoni,

    so now one things is not know that what will be query if i do not use lambda.

    If you don't want to use lambda, you can use the following code to query data.

    var query = from item in Items
                orderby item.Col1 descending, item.Col2 descending
                select item;

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 9, 2017 3:18 AM

All replies

  • User-1223304583 posted

    Try: OrderBy(x => x.Col1).ThenBy(x => x.Col2)

    Or For Descending try this: OrderByDescending (x => x.Col1).ThenByDescending (x => x.Col2)

    so now one things is not know that what will be query if i do not use lambda.

    thanks

    Wednesday, February 8, 2017 12:04 PM
  • User1930269528 posted

    There are methods for ordering the result descending as well, you can use OrderByDescending and ThenByDescending according to your need.

    Wednesday, February 8, 2017 12:23 PM
  • User1771544211 posted

    Hi mamoni,

    so now one things is not know that what will be query if i do not use lambda.

    If you don't want to use lambda, you can use the following code to query data.

    var query = from item in Items
                orderby item.Col1 descending, item.Col2 descending
                select item;

    Best Regards,

    Jean

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 9, 2017 3:18 AM