fork download
  1. using System.Linq;
  2.  
  3. class Person
  4. {
  5. public int Id;
  6. public string Name;
  7. }
  8.  
  9. class Address
  10. {
  11. public int Id;
  12. public string Street;
  13. }
  14.  
  15. class PersonAddress
  16. {
  17. public int PersonId, AddressId;
  18. }
  19.  
  20. public class Program
  21. {
  22. public static void Main(string[] args)
  23. {
  24. var personList = new []
  25. {
  26. new Person { Id = 1, Name = "Pete" },
  27. new Person { Id = 2, Name = "Mary" },
  28. new Person { Id = 3, Name = "Joe" }
  29. };
  30.  
  31. var addressList = new []
  32. {
  33. new Address { Id = 100, Street = "Home Lane" },
  34. new Address { Id = 101, Street = "Church Way" },
  35. new Address { Id = 102, Street = "Sandy Blvd" }
  36. };
  37.  
  38. var relations = new []
  39. {
  40. new PersonAddress { PersonId = 1, AddressId = 101 },
  41. new PersonAddress { PersonId = 3, AddressId = 101 },
  42.  
  43. new PersonAddress { PersonId = 2, AddressId = 102 },
  44. new PersonAddress { PersonId = 2, AddressId = 100 }
  45. };
  46.  
  47. var joined =
  48. from par in relations
  49. join p in personList
  50. on par.PersonId equals p.Id
  51. join a in addressList
  52. on par.AddressId equals a.Id
  53. select new { Person = p, Address = a };
  54.  
  55. foreach (var record in joined)
  56. System.Console.WriteLine("{0} lives on {1}", record.Person.Name, record.Address.Street);
  57. }
  58. }
  59.  
Success #stdin #stdout 0.03s 37288KB
stdin
Standard input is empty
stdout
Pete lives on Church Way
Joe lives on Church Way
Mary lives on Sandy Blvd
Mary lives on Home Lane