fork(1) download
  1. using System;
  2. using System.Linq;
  3.  
  4. public class Pet
  5. {
  6. public string Name { get; set; }
  7. public string Type { get; set; }
  8. public string Owner { get; set; }
  9. }
  10.  
  11. public class Owner
  12. {
  13. public string Name { get; set; }
  14. public string Country { get; set; }
  15. }
  16.  
  17. public class Test
  18. {
  19. public static void Main()
  20. {
  21.  
  22. Pet[] pets =
  23. {
  24. new Pet() { Name = "Vasya", Type = "Cat", Owner = "John" },
  25. new Pet() { Name = "Borya", Type = "Dog", Owner = "Dean" },
  26. new Pet() { Name = "Kim", Type = "Hedgehog", Owner = "Mary" },
  27. new Pet() { Name = "Joka", Type = "Dog", Owner = "John" },
  28. new Pet() { Name = "Mursick", Type = "Cat", Owner = "Dean" },
  29. new Pet() { Name = "Mick", Type = "Cat", Owner = "Mary" },
  30. new Pet() { Name = "John", Type = "Hedgehog", Owner = "John" },
  31. new Pet() { Name = "Jynx", Type = "Dog", Owner = "Dean" }
  32. };
  33.  
  34. Owner[] owners =
  35. {
  36. new Owner() { Name = "John", Country = "USA" },
  37. new Owner() { Name = "Mary", Country = "Switzerland" },
  38. new Owner() { Name = "Dean", Country = "Great Britain" }
  39. };
  40.  
  41. var el = from o in owners
  42. join p in pets
  43. on o.Name equals p.Owner
  44. group p by new {o.Country, p.Type}
  45. into grp select new { Country = grp.Key.Country, Type = grp.Key.Type, Count = grp.Count() };
  46.  
  47. foreach(var e in el) {
  48. Console.WriteLine(e.Country + " "+ e.Type+" "+e.Count);
  49. }
  50. }
  51. }
Success #stdin #stdout 0.04s 16124KB
stdin
Standard input is empty
stdout
USA  Cat  1
USA  Dog  1
USA  Hedgehog  1
Switzerland  Hedgehog  1
Switzerland  Cat  1
Great Britain  Dog  2
Great Britain  Cat  1