fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7. class Employee
  8. {
  9. public Employee(string name, string id, int salary, string department)
  10. {
  11. Name = name;
  12. Id = id;
  13. Salary = salary;
  14. Department = department;
  15. }
  16.  
  17. public string Name { get; private set; }
  18. public string Id { get; private set; }
  19. public int Salary { get; private set; }
  20. public string Department { get; private set; }
  21.  
  22. public override string ToString()
  23. {
  24. return String.Format("{0, -25}\t{1}\t{2}", Name, Id, Salary);
  25. }
  26. }
  27.  
  28. private static void Main(string[] args)
  29. {
  30. var employees = new List<Employee>
  31. {
  32. new Employee("Tyler Bennett", "E10297", 32000, "D101"),
  33. new Employee("John Rappl", "E21437", 47000, "D050"),
  34. new Employee("George Woltman", "E21437", 53500, "D101"),
  35. new Employee("Adam Smith", "E21437", 18000, "D202"),
  36. new Employee("Claire Buckman", "E39876", 27800, "D202"),
  37. new Employee("David McClellan", "E04242", 41500, "D101"),
  38. new Employee("Rich Holcomb", "E01234", 49500, "D202"),
  39. new Employee("Nathan Adams", "E41298", 21900, "D050"),
  40. new Employee("Richard Potter", "E43128", 15900, "D101"),
  41. new Employee("David Motsinger", "E27002", 19250, "D202"),
  42. new Employee("Tim Sampair", "E03033", 27000, "D101"),
  43. new Employee("Kim Arlich", "E10001", 57000, "D190"),
  44. new Employee("Timothy Grove", "E16398", 29900, "D190")
  45. };
  46.  
  47. DisplayTopNPerDepartment(employees, 2);
  48. }
  49.  
  50. static void DisplayTopNPerDepartment(IEnumerable<Employee> employees, int n)
  51. {
  52. var topSalariesByDepartment =
  53. from employee in employees
  54. group employee by employee.Department
  55. into g
  56. select new
  57. {
  58. Department = g.Key,
  59. TopEmployeesBySalary = g.OrderByDescending(e => e.Salary).Take(n)
  60. };
  61.  
  62. foreach (var x in topSalariesByDepartment)
  63. {
  64. Console.WriteLine("Department: " + x.Department);
  65. foreach (var employee in x.TopEmployeesBySalary)
  66. Console.WriteLine(employee);
  67. Console.WriteLine("----------------------------");
  68. }
  69. }
  70. }
Success #stdin #stdout 0.06s 34080KB
stdin
Standard input is empty
stdout
Department: D101
George Woltman           	E21437	53500
David McClellan          	E04242	41500
----------------------------
Department: D050
John Rappl               	E21437	47000
Nathan Adams             	E41298	21900
----------------------------
Department: D202
Rich Holcomb             	E01234	49500
Claire Buckman           	E39876	27800
----------------------------
Department: D190
Kim Arlich               	E10001	57000
Timothy Grove            	E16398	29900
----------------------------