A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

C# program to sort a list of employees based on salary using Linq
Q:

C# program to sort a list of employees based on salary using Linq

0

C# program to sort a list of employees based on salary using Linq

All Answers

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

Program:

The source code to sort a list of employees based on salary using Linq is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

//C# program to sort the list of employees 
//based on salary.

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

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

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

    static void Main(string[] args)
    {
        List<Employee> employees = new List<Employee>()
        {
             new Employee {ID=101,   Name="Amit  "    , Salary=4000},
             new Employee {ID=102,   Name="Gautam"    , Salary=6000},
             new Employee {ID=103,   Name="Salman"    , Salary=3000},
             new Employee {ID=104,   Name="Ram   "    , Salary=2000},
        };


        var result = employees.OrderBy(sal => sal.Salary);

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

Output:

ID  Name   Salary
==================
104 Ram    2000
103 Salman 3000
101 Amit   4000
102 Gautam 6000
==================
Press any key to continue . . .

Explanation:

In the above program, we created a class Demo that contains the Main() method. In the Main() method we created a list of employees with fields IDNameSalary.

var result = employees.OrderBy(sal => sal.Salary);

The above method call will return the sorted list of employees based on salary.

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

In the above code, we accessed the employee detail one by one and print on the console screen.

 

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now