fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestApp
  6. {
  7.  
  8. class Program
  9. {
  10.  
  11. public class Player
  12. {
  13. public string Position { get; set; }
  14. public string Name { get; set; }
  15. public int Salary { get; set; }
  16. public int Age { get; set; }
  17. }
  18.  
  19. static void Main(string[] args)
  20. {
  21.  
  22. List<Player> players = new List<Player>()
  23. {
  24. new Player() { Name = "Peyton Manning", Age = 36, Position = "QB", Salary = 19000000 },
  25. new Player() { Name = "Tom Brady", Age = 35, Position = "QB", Salary = 18400000 },
  26. new Player() { Name = "Drew Brees", Age = 34, Position = "QB", Salary = 21000000 },
  27. new Player() { Name = "Randy Moss", Age = 35, Position = "WR", Salary = 7000000 },
  28. new Player() { Name = "Marvin Harrison", Age = 38, Position = "WR", Salary = 11000000 },
  29. new Player() { Name = "Demaryius Thomas", Age = 23, Position = "WR", Salary = 5000000 },
  30. new Player() { Name = "Ryan Clady", Age = 26, Position = "OT", Salary = 10000000 },
  31. };
  32.  
  33. var highestPaidPlayers = from pl in players
  34. group pl by pl.Position
  35. into g
  36. let max = g.Max(x => x.Salary)
  37. let maxPl = g.Where(x => x.Salary == max).First()
  38. select maxPl;
  39.  
  40. var highestPaidPlayers2 =
  41. players.GroupBy(p => p.Position).Select(x => x.Where(p => p.Salary == x.Max(m => m.Salary)).First());
  42.  
  43.  
  44. foreach (Player player in highestPaidPlayers)
  45. {
  46. Console.WriteLine("The highest paid {0} is {1}, who is being paid {2}", player.Position, player.Name, player.Salary);
  47. }
  48.  
  49. System.Console.WriteLine("-----------");
  50.  
  51. foreach (Player player in highestPaidPlayers2)
  52. {
  53. Console.WriteLine("The highest paid {0} is {1}, who is being paid {2}", player.Position, player.Name, player.Salary);
  54. }
  55. }
  56. }
  57. }
Success #stdin #stdout 0.05s 34192KB
stdin
Standard input is empty
stdout
The highest paid QB is Drew Brees, who is being paid 21000000
The highest paid WR is Marvin Harrison, who is being paid 11000000
The highest paid OT is Ryan Clady, who is being paid 10000000
-----------
The highest paid QB is Drew Brees, who is being paid 21000000
The highest paid WR is Marvin Harrison, who is being paid 11000000
The highest paid OT is Ryan Clady, who is being paid 10000000