fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class Customer
  6. {
  7. public string ID { get; set; }
  8. public string Name { get; set; }
  9. public string Suburb { get; set; }
  10. public int Balance { get; set; }
  11. public int Year { get; set; }
  12.  
  13. public Customer(string a, string b, string c, int balance, int year)
  14. {
  15. ID = a;
  16. Name = b;
  17. Suburb = c;
  18. Balance = balance;
  19. Year = year;
  20. }
  21. }
  22.  
  23. public class Test
  24. {
  25. public static void Main()
  26. {
  27. var customers = new List<Customer>();
  28. customers.Add(new Customer("C0020", "Alfred", "Campbelltown", 1500, 2006));
  29. customers.Add(new Customer("C0021", "Ryder", "Liverpool", 2000, 2008));
  30. customers.Add(new Customer("C0022", "Alison", "Strathfield", 5500, 2012));
  31. customers.Add(new Customer("C0023", "Eliza", "Liverpool", 6000, 2012));
  32. customers.Add(new Customer("C0024", "Natsu", "Campbelltown", 2560, 2011));
  33.  
  34. var suburbGroups = customers
  35. .GroupBy(c => c.Suburb)
  36. .Select(g => new { Suburb = g.Key, Balance = g.Sum(c => c.Balance) })
  37. .OrderByDescending(x => x.Balance);
  38.  
  39. foreach (var grp in suburbGroups)
  40. Console.WriteLine("Suburb: {0} Total-Balance: {1}", grp.Suburb, grp.Balance);
  41. }
  42. }
Success #stdin #stdout 0.05s 37216KB
stdin
Standard input is empty
stdout
Suburb: Liverpool  Total-Balance: 8000
Suburb: Strathfield  Total-Balance: 5500
Suburb: Campbelltown  Total-Balance: 4060