C# program to sort a list of employees based on salary and whose department is ABC using LINQ
//C# program to sort the list of employees based on
//salary whose department is ABC.
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="Gautam" , Salary=6000,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.Where(emp=>emp.Department=="ABC").OrderBy(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
============================
103 Salman 3000 ABC
101 Amit 4000 ABC
105 Shyam 7000 ABC
============================
Press any key to continue . . .
Table of Contents