fork download
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Test
  5. {
  6. public static void Main()
  7. {
  8. var List = new []
  9. {
  10. new { tovar_code = 1, tovar_kol = 10, tovar_obyem = 20 },
  11. new { tovar_code = 2, tovar_kol = 20, tovar_obyem = 30 },
  12. new { tovar_code = 3, tovar_kol = 30, tovar_obyem = 60 },
  13. new { tovar_code = 4, tovar_kol = 40, tovar_obyem = 80 },
  14. new { tovar_code = 5, tovar_kol = 50, tovar_obyem = 90 }
  15. }.ToList();
  16.  
  17. var total_quantity = List.Sum(x => x.tovar_kol);
  18. var total_volume = List.Sum(x => x.tovar_obyem);
  19.  
  20. Console.WriteLine($"Total quantity: {total_quantity}, total volume: {total_volume}");
  21.  
  22. var result =
  23. List.Aggregate(
  24. new { total_quantity = 0, total_volume = 0 },
  25. (sum, curr) => new { total_quantity = sum.total_quantity + curr.tovar_kol,
  26. total_volume = sum.total_volume + curr.tovar_obyem });
  27.  
  28. Console.WriteLine($"Total quantity: {result.total_quantity}, total volume: {result.total_volume}");
  29.  
  30. int total_quantity_2 = 0;
  31. double total_volume_2 = 0;
  32. foreach (var item in List)
  33. {
  34. total_quantity_2 += item.tovar_kol;
  35. total_volume_2 += item.tovar_obyem;
  36. }
  37. Console.WriteLine($"Total quantity: {total_quantity_2}, total volume: {total_volume_2}");
  38. }
  39. }
Success #stdin #stdout 0.05s 24032KB
stdin
Standard input is empty
stdout
Total quantity: 150, total volume: 280
Total quantity: 150, total volume: 280
Total quantity: 150, total volume: 280