ADVERTISEMENT
ADVERTISEMENT

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

Here, we are going to learn about the Linq ThenBy() method and its C# implementation.
Submitted by Nidhi, on August 31, 2020

Here we will use Linq OrderBy() and ThenBy() method of List. Here OrderBy() method is used to sort employee detail at first level and then ThenBy() method is used to sort employee information at the second level.

Program:

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

//C# program to demonstrate the Linq ThenBy() 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.OrderBy(name => name.Name).ThenBy(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
============================
102 Amit   3000 XYZ
101 Amit   4000 ABC
106 Kishor 5000 XYZ
104 Ram    2000 XYZ
103 Salman 3000 ABC
105 Shyam  7000 ABC
============================
Press any key to continue . . .

Explanation:

In the above program, we created an Employee class that contains data members ID, Name, Salary, 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 using OrderBy() and ThenBy() method. The OrderBy() method is used for the first level of sorting and ThenBy() is used to sort at the next level in different fields. 

Note: The OrderBy() and ThenBy() methods are used for sorting in ascending order.

C# LINQ Programs »


ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.