fork download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. class Program
  6. {
  7. class B
  8. {
  9. public string Country { get; set; }
  10. public string Article { get; set; }
  11. public string Category { get; set; }
  12. }
  13.  
  14. class D
  15. {
  16. public int Price { get; set; }
  17. public string Store { get; set; }
  18. public string Article { get; set; }
  19. }
  20.  
  21. class E
  22. {
  23. public string Store { get; set; }
  24. public string Article { get; set; }
  25. public string ConsumerCode { get; set; }
  26. }
  27.  
  28. static void Main()
  29. {
  30. List<B> sequenceB = new List<B>
  31. {
  32. new B { Country = "Germany", Article = "A1", Category = "Tech" },
  33. new B { Country = "USA", Article = "A2", Category = "Gadget" },
  34. new B { Country = "Japan", Article = "A3", Category = "Tech" }
  35. };
  36.  
  37. List<D> sequenceD = new List<D>
  38. {
  39. new D { Price = 100, Store = "Shop1", Article = "A1" },
  40. new D { Price = 200, Store = "Shop2", Article = "A2" },
  41. new D { Price = 150, Store = "Shop1", Article = "A3" }
  42. };
  43.  
  44. List<E> sequenceE = new List<E>
  45. {
  46. new E { Store = "Shop1", Article = "A1", ConsumerCode = "C1" },
  47. new E { Store = "Shop1", Article = "A3", ConsumerCode = "C2" },
  48. new E { Store = "Shop2", Article = "A2", ConsumerCode = "C3" }
  49. };
  50.  
  51. var result = from e in sequenceE
  52. join d in sequenceD on e.Article equals d.Article
  53. join b in sequenceB on d.Article equals b.Article
  54. group d.Price by new { e.Store, b.Country } into g
  55. select new
  56. {
  57. Store = g.Key.Store,
  58. Country = g.Key.Country,
  59. TotalPrice = g.Sum()
  60. };
  61.  
  62. var allPairs = from e in sequenceE.Select(x => x.Store).Distinct()
  63. from b in sequenceB.Select(x => x.Country).Distinct()
  64. join r in result on new { Store = e, Country = b } equals new { r.Store, r.Country } into gj
  65. from r in gj.DefaultIfEmpty()
  66. orderby e, b
  67. select new
  68. {
  69. Store = e,
  70. Country = b,
  71. TotalPrice = r?.TotalPrice ?? 0
  72. };
  73.  
  74. foreach (var item in allPairs)
  75. {
  76. Console.WriteLine($"{item.Store} {item.Country} {item.TotalPrice}");
  77. }
  78. }
  79. }
Success #stdin #stdout 0.08s 32412KB
stdin
Standard input is empty
stdout
Shop1 Germany 100
Shop1 Japan 150
Shop1 USA 0
Shop2 Germany 0
Shop2 Japan 0
Shop2 USA 200