Query_LeftJoin_ByGroupJoin_Test.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System.Data;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. namespace Vitorm.MsTest.CommonTest
  4. {
  5. [TestClass]
  6. public class Query_LeftJoin_ByGroupJoin_Test
  7. {
  8. [TestMethod]
  9. public void Test_LeftJoin_Demo()
  10. {
  11. using var dbContext = DataSource.CreateDbContext();
  12. var userQuery = dbContext.Query<User>();
  13. // Linq Expression
  14. {
  15. var query =
  16. from user in userQuery
  17. join father in userQuery on user.fatherId equals father.id into fathers
  18. from father in fathers.DefaultIfEmpty()
  19. where user.id > 2
  20. orderby user.id
  21. select new { user, father };
  22. var userList = query.ToList();
  23. Assert.AreEqual(4, userList.Count);
  24. Assert.AreEqual(3, userList[0].user.id);
  25. Assert.AreEqual(5, userList[0].father?.id);
  26. Assert.AreEqual(4, userList[1].user.id);
  27. Assert.AreEqual(null, userList[1].father?.name);
  28. }
  29. // Lambda Expression
  30. {
  31. var query =
  32. userQuery.GroupJoin(
  33. userQuery
  34. , user => user.fatherId
  35. , father => father.id
  36. , (user, fathers) => new { user, fathers }
  37. )
  38. .SelectMany(
  39. row => row.fathers.DefaultIfEmpty()
  40. , (row, father) => new { row, father }
  41. )
  42. .Where(row2 => row2.row.user.id > 2)
  43. .OrderBy(row2 => row2.row.user.id)
  44. .Select(row2 => new { row2.row.user, row2.father });
  45. var userList = query.ToList();
  46. Assert.AreEqual(4, userList.Count);
  47. Assert.AreEqual(3, userList[0].user.id);
  48. Assert.AreEqual(5, userList[0].father?.id);
  49. Assert.AreEqual(4, userList[1].user.id);
  50. Assert.AreEqual(null, userList[1].father?.name);
  51. }
  52. }
  53. [TestMethod]
  54. public void Test_LeftJoin_Complex()
  55. {
  56. using var dbContext = DataSource.CreateDbContext();
  57. var userQuery = dbContext.Query<User>();
  58. // Linq Expression
  59. {
  60. var query =
  61. from user in userQuery
  62. join father in userQuery on user.fatherId equals father.id into fathers
  63. from father in fathers.DefaultIfEmpty()
  64. join mother in userQuery on user.motherId equals mother.id into mothers
  65. from mother in mothers.DefaultIfEmpty()
  66. where user.id > 2
  67. orderby user.id
  68. select new
  69. {
  70. user,
  71. father,
  72. mother,
  73. testId = user.id + 100,
  74. hasFather = father != null && father.name != null ? true : false
  75. };
  76. query = query.Skip(1).Take(2);
  77. var userList = query.ToList();
  78. Assert.AreEqual(2, userList.Count);
  79. var first = userList.First();
  80. Assert.AreEqual(4, first.user.id);
  81. Assert.AreEqual(null, first.father?.name);
  82. Assert.AreEqual(null, first.mother?.name);
  83. Assert.AreEqual(104, first.testId);
  84. Assert.AreEqual(false, first.hasFather);
  85. }
  86. }
  87. }
  88. }