fork(4) download
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace TestApp
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var list1 = new List<ItemOne>
  12. {
  13. new ItemOne {IDItem = 1, OneProperty = "1"},
  14. new ItemOne {IDItem = 2, OneProperty = null},
  15. new ItemOne {IDItem = 3, OneProperty = "3"},
  16. new ItemOne {IDItem = 4, OneProperty = "4"}
  17. };
  18. var list2 = new List<ItemTwo>
  19. {
  20. new ItemTwo {IDItem = 2, TwoProperty = "2"},
  21. new ItemTwo {IDItem = 3, TwoProperty = "3"},
  22. };
  23.  
  24. var query = from x in list1
  25. join y in list2 on x.IDItem equals y.IDItem
  26. into z
  27. from q in z.DefaultIfEmpty()
  28. select new {IOne = x, ITwo = q};
  29. foreach (var pair in query)
  30. {
  31. if (pair.ITwo != null) // && pair.IOne.OneProperty != null
  32. pair.IOne.OneProperty = pair.ITwo.TwoProperty;
  33. }
  34.  
  35. var resultList = query.Select(x => x.IOne).ToList();
  36.  
  37. foreach (var one in resultList)
  38. {
  39. Console.WriteLine(one.OneProperty);
  40. }
  41. System.Console.ReadLine();
  42.  
  43. }
  44.  
  45. public class ItemOne
  46. {
  47. public int IDItem { get; set; }
  48. public string OneProperty { get; set; }
  49. }
  50.  
  51. public class ItemTwo
  52. {
  53. public int IDItem { get; set; }
  54. public string TwoProperty { get; set; }
  55. }
  56. }
  57. }
  58.  
Success #stdin #stdout 0.06s 34136KB
stdin
Standard input is empty
stdout
1
2
3
4