Sobes.tech
Junior — Middle

How can you determine the department with the highest average salary in the employee table?

sobes.tech AI

Answer from AI

To find the department with the highest average salary in C#, you can use LINQ to group employees by department, calculate the average salary, and select the department with the maximum value.

Example:

var maxAvgSalaryDept = employees
    .GroupBy(e => e.Department)
    .Select(g => new { Department = g.Key, AvgSalary = g.Average(e => e.Salary) })
    .OrderByDescending(d => d.AvgSalary)
    .FirstOrDefault();

if (maxAvgSalaryDept != null)
{
    Console.WriteLine($"Department with highest average salary: {maxAvgSalaryDept.Department}, average salary: {maxAvgSalaryDept.AvgSalary}");
}

Here, employees is a collection of objects with Department and Salary fields. The code groups employees by department, computes the average salary, sorts descending, and picks the first (highest).

How can you determine the department with the highest… - sobes.tech