fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. public static void Main()
  8. {
  9. var list = new List<Test>
  10. {
  11. new Test { A = 1, B = 1, C = 1, GroupName = "A" },
  12. new Test { A = 1, B = 1, C = 1, GroupName = "A" },
  13.  
  14. new Test { A = 2, B = 2, C = 2, GroupName = "B" },
  15. new Test { A = 2, B = 2, C = 2, GroupName = "B" },
  16.  
  17. new Test { A = 3, B = 3, C = 3, GroupName = "C" },
  18. new Test { A = 3, B = 3, C = 3, GroupName = "C" },
  19. };
  20. var newList = list.GroupBy(g => g.GroupName).Select(e => new
  21. {
  22. A_Count = e.Sum(a => a.A),
  23. B_Count = e.Sum(b => b.B),
  24. C_Count = e.Sum(c => c.C),
  25. GroupName = e.Key
  26. });
  27.  
  28. foreach (var e in newList)
  29. {
  30. Console.WriteLine("groupName: {0}, A sum: {1}", e.GroupName, e.A_Count);
  31. }
  32. }
  33.  
  34. public int A { get; set; }
  35. public int B { get; set; }
  36. public int C { get; set; }
  37. public string GroupName { get; set; }
  38. }
Success #stdin #stdout 0.02s 132544KB
stdin
Standard input is empty
stdout
groupName: A, A sum: 2
groupName: B, A sum: 4
groupName: C, A sum: 6