Query_Group_Test.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System.Data;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. namespace Vitorm.MsTest.CommonTest
  4. {
  5. [TestClass]
  6. public class Query_Group_Test
  7. {
  8. [TestMethod]
  9. public void Test_Group_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. group user by new { user.fatherId, user.motherId } into userGroup
  18. select new { userGroup.Key.fatherId, userGroup.Key.motherId };
  19. var rows = query.ToList();
  20. Assert.AreEqual(3, rows.Count);
  21. Assert.AreEqual(0, rows.Select(u => u.fatherId).Except(new int?[] { 4, 5, null }).Count());
  22. Assert.AreEqual(0, rows.Select(u => u.motherId).Except(new int?[] { 6, null }).Count());
  23. }
  24. // Lambda Expression
  25. {
  26. var query =
  27. userQuery
  28. .GroupBy(user => new { user.fatherId, user.motherId })
  29. .Select(userGroup => new
  30. {
  31. userGroup.Key.fatherId,
  32. userGroup.Key.motherId
  33. })
  34. ;
  35. var rows = query.ToList();
  36. Assert.AreEqual(3, rows.Count);
  37. Assert.AreEqual(0, rows.Select(u => u.fatherId).Except(new int?[] { 4, 5, null }).Count());
  38. Assert.AreEqual(0, rows.Select(u => u.motherId).Except(new int?[] { 6, null }).Count());
  39. }
  40. }
  41. [TestMethod]
  42. public void Test_Group_Complex()
  43. {
  44. using var dbContext = DataSource.CreateDbContext();
  45. var userQuery = dbContext.Query<User>();
  46. // Linq Expression
  47. {
  48. var query =
  49. from user in userQuery.Where(u => u.id > 1)
  50. group user by new { user.fatherId, user.motherId } into userGroup
  51. where userGroup.Key.motherId != null && userGroup.Count() >= 1
  52. orderby userGroup.Key.fatherId descending, userGroup.Count() descending
  53. select new { userGroup.Key.fatherId, userGroup.Key.motherId, rowCount = userGroup.Count(), maxId = userGroup.Max(m => m.id) };
  54. query = query.Skip(1).Take(1);
  55. var rows = query.ToList();
  56. Assert.AreEqual(1, rows.Count);
  57. Assert.AreEqual(4, rows[0].fatherId);
  58. Assert.AreEqual(6, rows[0].motherId);
  59. Assert.AreEqual(1, rows[0].rowCount);
  60. Assert.AreEqual(2, rows[0].maxId);
  61. }
  62. // Lambda Expression
  63. {
  64. var query =
  65. userQuery
  66. .Where(u => u.id > 1)
  67. .GroupBy(user => new { user.fatherId, user.motherId })
  68. .Where(userGroup => userGroup.Key.motherId != null)
  69. .OrderByDescending(userGroup => userGroup.Key.fatherId)
  70. .Select(userGroup => new
  71. {
  72. userGroup.Key.fatherId,
  73. userGroup.Key.motherId,
  74. rowCount = userGroup.Count(),
  75. maxId = userGroup.Max(m => m.id)
  76. })
  77. .Skip(1)
  78. .Take(1)
  79. ;
  80. var rows = query.ToList();
  81. Assert.AreEqual(1, rows.Count);
  82. Assert.AreEqual(4, rows[0].fatherId);
  83. Assert.AreEqual(6, rows[0].motherId);
  84. Assert.AreEqual(1, rows[0].rowCount);
  85. Assert.AreEqual(2, rows[0].maxId);
  86. }
  87. }
  88. [TestMethod]
  89. public void Test_Others()
  90. {
  91. using var dbContext = DataSource.CreateDbContext();
  92. var userQuery = dbContext.Query<User>();
  93. {
  94. var query =
  95. userQuery
  96. .Where(user => user.id < 7)
  97. .GroupBy(user => new { user.fatherId, user.motherId })
  98. .OrderByDescending(group => group.Count())
  99. .Select(userGroup => new
  100. {
  101. userGroup.Key.fatherId,
  102. rowCount = userGroup.Count(),
  103. maxId = userGroup.Max(m => m.id),
  104. minId = userGroup.Min(m => m.id),
  105. sumId = userGroup.Sum(m => m.id),
  106. avgId = userGroup.Average(m => (double)m.id)
  107. })
  108. ;
  109. var rows = query.ToList();
  110. Assert.AreEqual(3, rows.Count);
  111. var row = rows[1];
  112. Assert.AreEqual(2, row.rowCount);
  113. Assert.AreEqual(2, row.maxId);
  114. Assert.AreEqual(1, row.minId);
  115. Assert.AreEqual(3, row.sumId);
  116. Assert.AreEqual(1.5, row.avgId);
  117. }
  118. {
  119. var query =
  120. userQuery
  121. .GroupBy(user => new { user.fatherId, user.motherId })
  122. .Where(userGroup => userGroup.Key.motherId != null)
  123. .OrderByDescending(userGroup => userGroup.Key.fatherId)
  124. .Select(userGroup => new { userGroup.Key.fatherId, userGroup.Key.motherId })
  125. ;
  126. var rows = query.ToList();
  127. Assert.AreEqual(2, rows.Count);
  128. Assert.AreEqual(5, rows[0].fatherId);
  129. }
  130. {
  131. var query =
  132. userQuery
  133. .GroupBy(user => user.fatherId)
  134. .Where(userGroup => userGroup.Key != null)
  135. .OrderByDescending(userGroup => userGroup.Key)
  136. .Select(userGroup => new { fatherId = userGroup.Key, rowCount = userGroup.Count() })
  137. ;
  138. var rows = query.ToList();
  139. Assert.AreEqual(2, rows.Count);
  140. Assert.AreEqual(5, rows[0].fatherId);
  141. }
  142. }
  143. }
  144. }