Q:

C# program to demonstrate the example of Linq ThenByDescending() method

belongs to collection: C# LINQ Programs

0

C# program to demonstrate the example of Linq ThenByDescending() method

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to demonstrate the Linq ThenByDescending() method, is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to demonstrate the Linq ThenByDescending() method.

using System;
using System.Linq;
using System.Collections.Generic;

public class Employee
{
    int ID;
    string Name;
    int Salary;
    string Department;

    public override string ToString()
    {
        return ID + " " + Name + " " + Salary + " " + Department;
    }

    static void Main(string[] args)
    {
        List<Employee> employees = new List<Employee>()
        {
             new Employee {ID=101,   Name="Amit  "    , Salary=4000,Department="ABC"},
             new Employee {ID=102,   Name="Amit  "    , Salary=3000,Department="XYZ"},
             new Employee {ID=103,   Name="Salman"    , Salary=3000,Department="ABC"},
             new Employee {ID=104,   Name="Ram   "    , Salary=2000,Department="XYZ"},
             new Employee {ID=105,   Name="Shyam "    , Salary=7000,Department="ABC"},
             new Employee {ID=106,   Name="Kishor"    , Salary=5000,Department="XYZ"},
        };


        var result = employees.OrderByDescending(name => name.Name).ThenByDescending(sal=>sal.Salary);


        Console.WriteLine("ID  Name  Salary  Department");
        Console.WriteLine("============================");
        foreach (Employee emp in result)
        {
            Console.WriteLine(emp.ToString());
        }
        Console.WriteLine("============================");
    }
}

Output:

ID  Name  Salary  Department
============================
105 Shyam  7000 ABC
103 Salman 3000 ABC
104 Ram    2000 XYZ
106 Kishor 5000 XYZ
101 Amit   4000 ABC
102 Amit   3000 XYZ
============================
Press any key to continue . . .

Explanation:

In the above program, we created an Employee class that contains data members IDNameSalary, and Department, and Employee class also contains static method Main(). The Main() method is the entry point of the program.

In the Main() method, we created the list of employees using List collection. Then we sorted the list of employees in descending order using OrderByDescending() and ThenByDescending() method. The OrderByDescending() method is used for the first level of sorting and ThenByDescending() is used to sort at the next level in different fields.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# LINQ Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to demonstrate the example of Linq Reve... >>
<< C# program to demonstrate the example of Linq Then...