Query_LeftJoin_ByGroupJoin_Test.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using Vit.Extensions.Vitorm_Extensions;
  3. using System.Data;
  4. namespace Vitorm.MsTest.CommonTest
  5. {
  6. [TestClass]
  7. public class Query_LeftJoin_ByGroupJoin_Test
  8. {
  9. [TestMethod]
  10. public void Test_LeftJoin_Demo()
  11. {
  12. using var dbContext = DataSource.CreateDbContext();
  13. var userQuery = dbContext.Query<User>();
  14. // Linq Expresssion
  15. {
  16. var query =
  17. from user in userQuery
  18. join father in userQuery on user.fatherId equals father.id into fathers
  19. from father in fathers.DefaultIfEmpty()
  20. where user.id > 2
  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?.id);
  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. .Select(row2 => new { row2.row.user, row2.father });
  45. var sql = query.ToExecuteString();
  46. var userList = query.ToList();
  47. Assert.AreEqual(4, userList.Count);
  48. //Assert.AreEqual(3, userList[0].user.id);
  49. //Assert.AreEqual(5, userList[0].father?.id);
  50. //Assert.AreEqual(4, userList[1].user.id);
  51. //Assert.AreEqual(null, userList[1].father?.id);
  52. }
  53. }
  54. [TestMethod]
  55. public void Test_LeftJoin_Complex()
  56. {
  57. using var dbContext = DataSource.CreateDbContext();
  58. var userQuery = dbContext.Query<User>();
  59. // Linq Expresssion
  60. {
  61. var query =
  62. from user in userQuery
  63. join father in userQuery on user.fatherId equals father.id into fathers
  64. from father in fathers.DefaultIfEmpty()
  65. join mother in userQuery on user.motherId equals mother.id into mothers
  66. from mother in mothers.DefaultIfEmpty()
  67. where user.id > 2
  68. orderby father.id descending
  69. select new
  70. {
  71. user,
  72. father,
  73. mother,
  74. testId = user.id + 100,
  75. hasFather = father != null ? true : false
  76. };
  77. query = query.Skip(1).Take(2);
  78. var sql = query.ToExecuteString();
  79. var userList = query.ToList();
  80. Assert.AreEqual(2, userList.Count);
  81. //var first = userList.First();
  82. //Assert.AreEqual(4, first.user.id);
  83. //Assert.AreEqual(null, first.father?.id);
  84. //Assert.AreEqual(null, first.mother?.id);
  85. //Assert.AreEqual(104, first.testId);
  86. //Assert.AreEqual(false, first.hasFather);
  87. }
  88. }
  89. }
  90. }