Query_LeftJoin_ByGroupJoin_Test.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 sql = query.ToExecuteString();
  23. var userList = query.ToList();
  24. Assert.AreEqual(4, userList.Count);
  25. Assert.AreEqual(3, userList[0].user.id);
  26. Assert.AreEqual(5, userList[0].father?.id);
  27. Assert.AreEqual(4, userList[1].user.id);
  28. Assert.AreEqual(null, userList[1].father?.name);
  29. }
  30. // Lambda Expression
  31. {
  32. var query =
  33. userQuery.GroupJoin(
  34. userQuery
  35. , user => user.fatherId
  36. , father => father.id
  37. , (user, fathers) => new { user, fathers }
  38. )
  39. .SelectMany(
  40. row => row.fathers.DefaultIfEmpty()
  41. , (row, father) => new { row, father }
  42. )
  43. .Where(row2 => row2.row.user.id > 2)
  44. .OrderBy(row2 => row2.row.user.id)
  45. .Select(row2 => new { row2.row.user, row2.father });
  46. var sql = query.ToExecuteString();
  47. var userList = query.ToList();
  48. Assert.AreEqual(4, userList.Count);
  49. Assert.AreEqual(3, userList[0].user.id);
  50. Assert.AreEqual(5, userList[0].father?.id);
  51. Assert.AreEqual(4, userList[1].user.id);
  52. Assert.AreEqual(null, userList[1].father?.name);
  53. }
  54. }
  55. [TestMethod]
  56. public void Test_LeftJoin_Complex()
  57. {
  58. using var dbContext = DataSource.CreateDbContext();
  59. var userQuery = dbContext.Query<User>();
  60. // Linq Expression
  61. {
  62. var query =
  63. from user in userQuery
  64. join father in userQuery on user.fatherId equals father.id into fathers
  65. from father in fathers.DefaultIfEmpty()
  66. join mother in userQuery on user.motherId equals mother.id into mothers
  67. from mother in mothers.DefaultIfEmpty()
  68. where user.id > 2
  69. orderby user.id
  70. select new
  71. {
  72. user,
  73. father,
  74. mother,
  75. testId = user.id + 100,
  76. hasFather = father.name != null ? true : false
  77. };
  78. query = query.Skip(1).Take(2);
  79. var sql = query.ToExecuteString();
  80. var userList = query.ToList();
  81. Assert.AreEqual(2, userList.Count);
  82. var first = userList.First();
  83. Assert.AreEqual(4, first.user.id);
  84. Assert.AreEqual(null, first.father?.name);
  85. Assert.AreEqual(null, first.mother?.name);
  86. Assert.AreEqual(104, first.testId);
  87. Assert.AreEqual(false, first.hasFather);
  88. }
  89. }
  90. }
  91. }