Bitter.Core 在聚联/分页聚联查询的时候,采用原生的MSSQL, MYSQL 语句查询,做过复杂高级项目的人知道,原生的聚合查询代码执行效率更高,更快,更容易书写,开发量最少。
借助原生的MSSQL,MYSQL 客户端工具,将聚合查询的SELECT 语句写好,直接扔进Bitter ORM 框架就行了。
业务层只关心写好相对应的 SELECT SQL 语句. 如下代码示例:
#region //聚联条件分页查询 var type = 1; //非关键代码,用于下面演示代码用 var Score=100; var studentname = "H"; //非关键代码,用于下面演示代码用 var sql = @"SELECT score.FScore,student.FName as studentName,class.FName as className,grade.FName as gradeName FROM dbo.t_StudentScore score LEFT JOIN dbo.t_student student ON score.FStudentId = student.FID LEFT JOIN dbo.t_class class ON student.FClassId=class.FID LEFT JOIN dbo.t_Grade grade ON grade.FID=class.FGradeId "; PageQuery pq = new PageQuery(sql, null); pq.Where("1=1"); if (type == 1) { pq.Where("score.FScore>60 "); } if (type == 2) { pq.Where("score.FScore>60 and score.FScore<80 "); } if (type == 3) { pq.Where("score.FScore==@ParmeScore",new {ParmeScore=Score}}); } if (!string.IsNullOrEmpty(studentname)) { pq.Where(" student.FName like '%' + @FScoreName + '%'",new {FScoreName=studentname}); } //通过ThenAsc 方法指定字段排序 pq.ThenASC("score.FScore "); //通过ThenDESC 方法指定字段排序 pq.ThenDESC("student.FName"); //自己直接指定排序字段和排序关键词 pq.OrderBy("student.FAddTime desc"); //分页指定 Skip: 当前页,Take :每页数量 pq.Skip(1).Take(10); var dt = pq.ToDataTable(); //获取数据 var studentscount = pq.Count(); //获取当前条件下的数量 #endregion
非常关键:
/** * 非常关键(Notic): PageQuery pq = new PageQuery(sql,null) 中的 sql 变量 不能包含 WHERE 条件,但是 LEFT JOININNER JOINUNION JOINOUTER JOIN 的子语句的 WHERE 条件是可以出现的,需要 WHERE ,统一通过 pq.Where() 进行条件加载, * 如果有疑问,可以 github 上留言,或者直接留言,Tks. 例如(正确):sql=@" SELECT score.FScore,student.FName,class.FName,grade.FName FROM dbo.t_StudentScore score LEFT JOIN dbo.t_student student ON score.FStudentId = student.FID LEFT JOIN dbo.t_class class ON student.FClassId=class.FID LEFT JOIN dbo.t_Grade grade ON grade.FID=class.FGradeId"; 例如(正确):sql=@" SELECT score.FScore,student.FName,class.FName,grade.FName FROM dbo.t_StudentScore score LEFT JOIN dbo.t_student student ON score.FStudentId = student.FID LEFT JOIN dbo.t_class class ON student.FClassId=class.FID LEFT JOIN (SELECT * FROM t_Grade WHERE FScore>15) grade ON grade.FID=class.FGradeId "; 例如(错误):sql=@" SELECT score.FScore,student.FName,class.FName,grade.FName FROM dbo.t_StudentScore score LEFT JOIN dbo.t_student student ON score.FStudentId = student.FID LEFT JOIN dbo.t_class class ON student.FClassId=class.FID LEFT JOIN dbo.t_Grade grade ON grade.FID=class.FGradeId WHERE score.FScore>10 AND score.FScore<60"; * * */