Middle+
It is required to output a list of employees receiving the maximum salary in their department.
sobes.tech AI
Answer from AI
To output a list of employees receiving the highest salary in their department, you can use LINQ with grouping by departments and selecting employees with the maximum salary in each group.
Example in C#:
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string Department { get; set; }
public decimal Salary { get; set; }
}
var employees = new List<Employee>
{
new Employee { Id = 1, Name = "Ivan", Department = "IT", Salary = 100000 },
new Employee { Id = 2, Name = "Maria", Department = "IT", Salary = 120000 },
new Employee { Id = 3, Name = "Peter", Department = "HR", Salary = 90000 },
new Employee { Id = 4, Name = "Olga", Department = "HR", Salary = 90000 },
};
var topEarners = employees
.GroupBy(e => e.Department)
.SelectMany(g =>
{
var maxSalary = g.Max(e => e.Salary);
return g.Where(e => e.Salary == maxSalary);
});
foreach (var emp in topEarners)
{
Console.WriteLine($"{emp.Name} from {emp.Department} with salary {emp.Salary}");
}
This code groups employees by departments, finds the maximum salary in each group, and selects all employees with that salary.