64
1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
5
6
public class Program
7
{
8
public static void Main()
9
{
10
List<Employee> employees = new List<Employee>
11
{
12
new Employee { ID = 1, Name = "Alice Johnson", Position = "Manager", Department = "Sales", HireDate = new DateTime(2018, 05, 23), Salary = 90000 },
13
new Employee { ID = 2, Name = "Bob Smith", Position = "Salesperson", Department = "Sales", HireDate = new DateTime(2020, 03, 15), Salary = 60000 },
14
new Employee { ID = 3, Name = "Charlie Brown", Position = "Developer", Department = "IT", HireDate = new DateTime(2019, 02, 11), Salary = 80000 },
15
new Employee { ID = 4, Name = "Diane Miller", Position = "Manager", Department = "IT", HireDate = new DateTime(2017, 04, 29), Salary = 95000 },
16
new Employee { ID = 5, Name = "Evan Rogers", Position = "Salesperson", Department = "Sales", HireDate = new DateTime(2021, 01, 09), Salary = 65000 }
17
};
18
19
// Step 1: Identify the department with the highest average salary
20
var highestAvgSalaryDept = employees
21
.GroupBy(e => e.Department)
22
.Select(group => new { Department = group.Key, AverageSalary = group.Average(e => e.Salary) })
23
.OrderByDescending(result => result.AverageSalary)
24
.FirstOrDefault();
Cached Result
The tag is about to be removed. There is a input here