fork download
  1. using static System.Console;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. class Customer {
  6. public int ID { get; set; }
  7. public string Name { get; set; }
  8. }
  9.  
  10. class Order {
  11. public int ID { get; set; }
  12. public string Product { get; set; }
  13. }
  14.  
  15. public static class Program {
  16. public static void Main() {
  17. // Example customers.
  18. var customers = new Customer[] {
  19. new Customer{ID = 5, Name = "Sam"},
  20. new Customer{ID = 6, Name = "Dave"},
  21. new Customer{ID = 7, Name = "Julia"},
  22. new Customer{ID = 8, Name = "Sue"}
  23. };
  24.  
  25. // Example orders.
  26. var orders = new Order[] {
  27. new Order{ID = 5, Product = "Book"},
  28. new Order{ID = 6, Product = "Game"},
  29. new Order{ID = 7, Product = "Computer"},
  30. new Order{ID = 8, Product = "Shirt"}
  31. };
  32.  
  33. // Join on the ID properties.
  34. var query = from c in customers
  35. join o in orders on c.ID equals o.ID
  36. select new { c.Name, o.Product };
  37.  
  38. WriteLine(query.GetType());
  39.  
  40. // Display joined groups.
  41. foreach (var group in query) {
  42. WriteLine("{0} bought {1}", group.Name, group.Product);
  43. }
  44. var lista = query.ToList();
  45. WriteLine(lista.GetType());
  46.  
  47. //criando explicitamente uma lista de string (neste caso é possível)
  48. var query2 = from c in customers
  49. join o in orders on c.ID equals o.ID
  50. select new List<string> { c.Name, o.Product };
  51.  
  52. WriteLine(query2.GetType());
  53.  
  54.  
  55. foreach (var group in query2) {
  56. WriteLine("{0} bought {1}", group[0], group[1]);
  57. }
  58. var lista2 = query2.ToList();
  59. WriteLine(lista2.GetType());
  60. }
  61. }
  62.  
  63. //https://pt.stackoverflow.com/q/48172/101
Success #stdin #stdout 0.02s 17252KB
stdin
Standard input is empty
stdout
System.Linq.Enumerable+<JoinIterator>d__81`4[Customer,Order,System.Int32,<>__AnonType0`2[System.String,System.String]]
Sam bought Book
Dave bought Game
Julia bought Computer
Sue bought Shirt
System.Collections.Generic.List`1[<>__AnonType0`2[System.String,System.String]]
System.Linq.Enumerable+<JoinIterator>d__81`4[Customer,Order,System.Int32,System.Collections.Generic.List`1[System.String]]
Sam bought Book
Dave bought Game
Julia bought Computer
Sue bought Shirt
System.Collections.Generic.List`1[System.Collections.Generic.List`1[System.String]]