You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
6.0 KiB

2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SqlSugar;
  7. using System.Linq.Expressions;
  8. using OrmTest.Models;
  9. namespace OrmTest.UnitTest
  10. {
  11. public class SelectQuery : UnitTestBase
  12. {
  13. private SelectQuery() { }
  14. public SelectQuery(int eachCount)
  15. {
  16. this.Count = eachCount;
  17. }
  18. internal void Init()
  19. {
  20. base.Begin();
  21. for (int i = 0; i < base.Count; i++)
  22. {
  23. Q2();
  24. }
  25. base.End("Method Test");
  26. }
  27. public void Q2()
  28. {
  29. using (var db = GetInstance())
  30. {
  31. //db.Database.IsEnableLogEvent = true;
  32. db.Ado.LogEventStarting = (sql, pars) =>
  33. {
  34. Console.WriteLine(sql + " " + pars);
  35. };
  36. #region dr ot entity
  37. db.IgnoreColumns.Add("TestId", "Student");
  38. var s1 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Name = it.Name, Student = it }).ToList();
  39. var s2 = db.Queryable<Student>().Select(it => new { id = it.Id, w = new { x = it } }).ToList();
  40. var s3 = db.Queryable<Student>().Select(it => new { newid = it.Id }).ToList();
  41. var s4 = db.Queryable<Student>().Select(it => new { newid = it.Id, obj = it }).ToList();
  42. var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent2 { Student = it, Name = it.Name }).ToList();
  43. #endregion
  44. #region sql and parameters validate
  45. var t1 = db.Queryable<Student, School>((st, sc) => new object[] {
  46. JoinType.Inner,st.Id==sc.Id
  47. }).GroupBy(st => st.Id).Having(st => SqlFunc.AggregateAvg(st.Id) == 1).Select(st => new { avgId = SqlFunc.AggregateAvg(st.Id) }).ToSql();
  48. base.Check("SELECT AVG([st].[ID]) AS [avgId] FROM [STudent] st Inner JOIN [School] sc ON ( [st].[ID] = [sc].[Id] ) GROUP BY [st].[ID] HAVING (AVG([st].[ID]) = @Const0 ) ",
  49. new List<SugarParameter>() {
  50. new SugarParameter("@Const0",1)
  51. }
  52. ,
  53. t1.Key, t1.Value, " select t1 Error");
  54. var t2 = db.Queryable<School, School>((st, st2) => new object[] {
  55. JoinType.Left,st.Id==st2.Id
  56. })
  57. .Where(st => st.Id > 0)
  58. .Select((st, st2) => new { stid = st.Id, scId = st2.Id, xx = st }).ToSql();
  59. base.Check("SELECT [st].[Id] AS [stid] , [st2].[Id] AS [scId] , [st].[Id] AS [School.Id] , [st].[Name] AS [School.Name] FROM [School] st Left JOIN [School] st2 ON ( [st].[Id] = [st2].[Id] ) WHERE ( [st].[Id] > @Id0 ) "
  60. , new List<SugarParameter>() {
  61. new SugarParameter("@Id0",0)
  62. }, t2.Key, t2.Value, "select t2 Error");
  63. var t3 = db.Queryable<Student, School, School>((st, sc, sc2) => new object[] {
  64. JoinType.Left,st.SchoolId==sc.Id,
  65. JoinType.Left,sc2.Id==sc.Id
  66. }).Where(st => st.Id > 0)
  67. .Select<School>((st) => new School() { Id = st.Id }).ToSql();
  68. base.Check("SELECT [st].[ID] AS [Id] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
  69. new List<SugarParameter>() {
  70. new SugarParameter("@Id0",0)
  71. }, t3.Key, t3.Value, "select t3 Error");
  72. db.Ado.IsEnableLogEvent = true;
  73. db.Ado.LogEventStarting = (sql, pars) =>
  74. {
  75. base.Check(" SELECT COUNT(1) FROM (SELECT [st].[ID] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) GROUP BY [st].[ID] ) CountTable ",
  76. null, sql, null, "select t4 Error");
  77. };
  78. var t4 = db.Queryable<Student, School, School>((st, sc, sc2) => new object[] {
  79. JoinType.Left,st.SchoolId==sc.Id,
  80. JoinType.Left,sc2.Id==sc.Id
  81. }).GroupBy(st => st.Id).Select(st => st.Id).Count();
  82. DateTime? result = DateTime.Now;
  83. var t5 = db.Queryable<Student>().Where(it => it.CreateTime > result.Value.Date).ToSql();
  84. base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [CreateTime] > @Const0 )",
  85. new List<SugarParameter>() {
  86. new SugarParameter("@Const0",result.Value.Date)
  87. }, t5.Key, t5.Value, "select t5 Error");
  88. db.Ado.IsEnableLogEvent = false;
  89. var t6 = db.Queryable<DataTestInfo2>().Where(it => SqlFunc.HasValue(it.Bool2) == false).ToSql();
  90. base.Check("SELECT [PK],[Bool1],[Bool2],[Text1] FROM [DataTestInfo2] WHERE (( CASE WHEN ( [Bool2]<>'' AND [Bool2] IS NOT NULL ) THEN 1 ELSE 0 END ) = @Const0 )",
  91. new List<SugarParameter>() {
  92. new SugarParameter("@Const0",false)
  93. }, t6.Key, t6.Value, "select t6 Error");
  94. var t7 = db.Queryable<Student>().Select(it=>new DataTestInfo2() {
  95. Bool1=SqlFunc.IIF(SqlFunc.Subqueryable<Student>().Where(x=>x.Id
  96. ==it.Id).Any(),true,false)
  97. }).ToSql();
  98. base.Check("SELECT ( CASE WHEN (EXISTS ( SELECT * FROM [STudent] WHERE ( [ID] = [it].[ID] ) )) THEN @MethodConst0 ELSE @MethodConst1 END ) AS [Bool1] FROM [STudent] it ",
  99. new List<SugarParameter>() {
  100. new SugarParameter("@MethodConst0",true),
  101. new SugarParameter("@MethodConst1",false)
  102. }, t7.Key, t7.Value, "select t7 Error");
  103. #endregion
  104. }
  105. }
  106. }
  107. }