locked
dynamic sorting RRS feed

  • Question

  • User527076549 posted

    HI,

    i want to query result and display in order based on Id,Nam,Phone and Email can anyone help me how to do it. expected result

    table1result=> table1.OrderBy(c => c.Id).ThenBy(c => c.Name).ThenBy(c => c.table2.Name).ThenBy(c=>c.table2.Phone)

    below is sample code which results in

      Func<IQueryable<T>, IOrderedQueryable<T>> orderExpression = o => o.OrderBy(a => a.Id);
    switch(case)
    {
     case "Id" : orderExpression = o => o.OrderBy(a => a.Id);
    break;
        case "name" : orderExpression = o => o.OrderBy(a => a.Name);
    break;
       case "phone" : orderExpression = o => o.OrderBy(a => a.table2.phone);
    break;
    .
    .
    .
    .
    }
    which result in 
    table1result=> table1.OrderBy(a => a.Id);
    

    Thanks and Really appreciate your help

    Friday, July 31, 2020 3:54 PM

All replies

  • User303363814 posted

    I'm not 100% sure what you are trying to do but, for example, change the case "name" to

    orderExpression = orderExpression.ThenBy(a => a.Name)

    should compound your queries.

    Saturday, August 1, 2020 12:38 AM
  • User527076549 posted

    Yes, when i try to do that

    orderExpression = orderExpression.ThenBy(a => a.Name), gives error saying Then by is not supported in 

    Func<IQueryable<T>, IOrderedQueryable<T>>

    Saturday, August 1, 2020 6:05 AM
  • User1535942433 posted

    Hi amithashenoy,

    As far as I think,you must use orderby before using Thenby.

    I don't understand your requirment clearly.Do you haven't know which field you have?If you have Id and Phone,you could orderby Id then by phone.

    Could you tell us more details of your requirment?

    Best regards,

    Yijing Sun

    Tuesday, August 4, 2020 7:00 AM