I have a list that I need sorted by two fields. I've tried using OrderByDescending in LINQ but that only allows me to specify one field. I'm looking for the list to be sorted by the first field and then if there are any duplicates in the first field to sort by the second field.

1 Answers

  1. Aditya Kumar Patel
    Re:Multiple column sort in Descending order by using LINQ
    9/25/2018 5:34:21 PM

    You need to use ThenByDescending

     

    List<Employee> Employee = new List<Employee>(){
                new Employee() {  Name = "Suresh",ID  = 1},
                new Employee() {  Name = "Rohini", ID=  2 },
                  new Employee() {  Name = "Amit", ID=  3 },
                new Employee() {  Name = "Pankaj", ID = 4},
                new Employee() {  Name = "Suraj", ID = 5}};
     
    var result = Employee.OrderByDescending(x =>x.Name).ThenByDescending(x =>x.ID);
     

    I hope it will help to you.


Leave a answer

Make sure you enter the (*) required information where indicated. HTML code is not allowed.

you may also like