I have a list that I need sorted by two fields. I've tried using OrderBy 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 order by in LINQ Ankit Kumar
    9/24/2018 3:34:07 PM

    You need to use ThenBy


    List<Employee> Employee = new List<Employee>(){
                new Employee() {  Name = "Suresh", Skills  = "C"},
                new Employee() {  Name = "Rohini", Skills =  "SQL Server" },
                new Employee() {  Name = "Amit", Skills =  "SQL" },
                new Employee() {  Name = "Pankaj", Skills =  "ASP.NET" },
                new Employee() {  Name = "Suresh", Skills =  "MVC" }};
    var result = Employee.OrderBy(x =>x.Name).ThenBy(x => x.Skills);


    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