fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Test
  6. {
  7. public string Property1;
  8. public string Property2;
  9. public string Property3;
  10. public string Property4;
  11. public int Property5;
  12. public int Property6;
  13. public int Property7;
  14. public int Property8;
  15. }
  16.  
  17. public class Program
  18. {
  19. public static void Main()
  20. {
  21. List<Test> testList = new List<Test>();
  22.  
  23. testList.Add(new Test { Property1 = "A", Property2 = "B", Property3 = "C", Property4 = "D", Property5 = 1, Property6 = 2, Property7 = 3, Property8 = 4, });
  24. testList.Add(new Test { Property1 = "AA", Property2 = "BB", Property3 = "CC", Property4 = "DD", Property5 = 7, Property6 = 8, Property7 = 9, Property8 = 10, });
  25. testList.Add(new Test { Property1 = "A", Property2 = "B", Property3 = "C", Property4 = "Z", Property5 = 10, Property6 = 20, Property7 = 30, Property8 = 40, });
  26.  
  27. Console.WriteLine("Input:");
  28. Print(testList);
  29.  
  30. // string, string, string = Property1, Property2, Property3
  31. var dict = new Dictionary<Tuple<string, string, string>, List<Test>>();
  32.  
  33. foreach (var el in testList)
  34. {
  35. List<Test> list;
  36.  
  37. var key = Tuple.Create(el.Property1, el.Property2, el.Property3);
  38.  
  39. if (!dict.TryGetValue(key, out list))
  40. {
  41. list = new List<Test>();
  42. dict.Add(key, list);
  43. }
  44.  
  45. list.Add(el);
  46. }
  47.  
  48. var output = new List<Test>(dict.Count);
  49.  
  50. foreach (var kv in dict)
  51. {
  52. var list = kv.Value;
  53.  
  54. var el = new Test
  55. {
  56. Property1 = kv.Key.Item1,
  57. Property2 = kv.Key.Item2,
  58. Property3 = kv.Key.Item3,
  59. Property4 = list[0].Property4,
  60. };
  61.  
  62. output.Add(el);
  63.  
  64. for (int i = 0; i < list.Count; i++)
  65. {
  66. el.Property5 += list[i].Property5;
  67. el.Property6 += list[i].Property6;
  68. el.Property7 += list[i].Property7;
  69. el.Property8 += list[i].Property8;
  70. }
  71. }
  72.  
  73. Console.WriteLine();
  74. Console.WriteLine("Output:");
  75. Print(output);
  76. }
  77.  
  78. public static void Print(IEnumerable<Test> els)
  79. {
  80. foreach (var el in els)
  81. {
  82. Console.WriteLine("{0} {1} {2} {3} {4} {5} {6} {7}", el.Property1, el.Property2, el.Property3, el.Property4, el.Property5, el.Property6, el.Property7, el.Property8);
  83. }
  84. }
  85. }
Success #stdin #stdout 0.01s 131648KB
stdin
Standard input is empty
stdout
Input:
A B C D 1 2 3 4
AA BB CC DD 7 8 9 10
A B C Z 10 20 30 40

Output:
A B C D 11 22 33 44
AA BB CC DD 7 8 9 10