Bitter.Core系列四:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 查询

一: 单表模型驱动查询 如下示例代码演示:

// 根据ID 查询:
var studentquery = db.FindQuery<TStudentInfo>().QueryById(12);
#region 根据条件全量查询  学生姓名为 HJB 的同学
BList<TStudentInfo> students = db.FindQuery<TStudentInfo>().Where(p => p.FName == "HJB").Find();

// 根据条件批量查询  学生姓名为 HJB 的同学
TStudentInfo student_1 = db.FindQuery<TStudentInfo>().Where(p => p.FName == "HJB").Find().FirstOrDefault(); //此FirstOrDefault 重构过,为安全模式,数据库如果查不到数据,返回为空对象,避免返回 NULL.
if (student_1.FID > 0) //说明查询到数据
{

}
#endregion
#region 根据条件全量查询 ,查询到年龄大于等于15 岁的学生 

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage >=15).Find();

#endregion
#region 根据条件全量查询 ,查询到年龄大于等于15 岁 并且 年龄 小于 17岁 的学生

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage >= 15&&p.FAage<17).Find();
//上面的查询也可以写如下方式
students = db.FindQuery<TStudentInfo>().Where(p => p.FAage >= 15).Where(p => p.FAage < 17).Find(); //多级 Where 查询

#endregion
#region 根据条件全量查询 ,查询到年龄<>15

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage != 15).Find();

#endregion
#region 根据条件全量查询,查询到名字包含 "H" 的学生 

students = db.FindQuery<TStudentInfo>().Where(p => p.FName.Contains("H")).Find(); //Contains 运行最终 Sql 为 : '%H%',暂时不支持 'H%','%H'

#endregion
#region 根据条件全量查询,多级 Where  查询

var studentquery = db.FindQuery<TStudentInfo>().Where(p => p.FName.Contains("H")); //Contains 运行最终 Sql 为 : '%H%',暂时不支持 'H%','%H'
studentquery.Where(p=>p.FAage>15);
students = studentquery.Find();

#endregion


二:高级查询直接SQL语句查询(非分页)

#region 高级查询直接SQL语句查询(非分页)
//查出分数>=90分的学生姓名以及具体学分

DataTable dt=  db.FindQuery(
                   @"SELECT score.FScore,student.FName as studentNameFROM  t_StudentScore score
                    LEFT JOIN t_student student  ON score.FStudentId = student.FID
                    WHERE score.FScore>=@score
                  ", new { score = 90 }).Find();
#endregion


三:单表模型驱动查询数量:

#region 根据条件全量查询 ,查询到年龄<>15 的总数量

var count = db.FindQuery<TStudentInfo>().Where(p => p.FAage != 15).FindCount();

#endregion


四: 单表模型驱动查询 排序:


1:正序
#region  ASC  正序 

students = db.FindQuery<TStudentInfo>().Where(p=>p.FAage>15).ThenAsc(p=>p.FAddTime).Find();

#endregion

2:正序
#region  DESC  正序 

students = db.FindQuery<TStudentInfo>().Where(p=>p.FAage>15).ThenDesc(p=>p.FAddTime).Find();

#endregion

3:多级排序(DESC)
#region  DESC  多级正序 

students = db.FindQuery<TStudentInfo>().Where(p=>p.FAage>15).ThenDesc(p=>p.FAage).ThenDesc(p=>p.FAddTime).Find();

#endregion

4:多级排序(ASC)
#region  ASC  多级正序 

students = db.FindQuery<TStudentInfo>().Where(p=>p.FAage>15).ThenAsc(p=>p.FAage).ThenAsc(p=>p.FAddTime).Find();

#endregion

5:多级正序倒序混排(ASC-DESC)
#region  (ASC-DESC)  多级正序倒序混排

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage > 15).ThenAsc(p => p.FAage).ThenDesc(p => p.FAddTime).Find();

#endregion



五:单表模型驱动查询--只查询符合条件的前 N 条数据:

#region  (ASC-DESC)  多级正序倒序混排

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage > 15).ThenAsc(p => p.FAage).ThenDesc(p => p.FAddTime).SetSize(10).Find(); //后面的 SetSize(N)  方法指定了需要查询的前 N 条数量

#endregion


六:单表模型驱动查询--只查询符合条件的前 N 条数据,并且只返回具体的列(FAage,FName):

#region  单表模型驱动查询--只查询符合条件的前 N 条数据,并且只返回具体的列(FAage,FName):

students = db.FindQuery<TStudentInfo>().Where(p => p.FAage > 15).ThenAsc(p => p.FAage).ThenDesc(p => p.FAddTime).SetSize(10).Select(c=>new object[] { c.FAage,c.FName}).Find(); //后面的 Select(columns)  方法指定了需要查询的列
students = db.FindQuery<TStudentInfo>().Where(p => p.FAage > 15).ThenAsc(p => p.FAage).ThenDesc(p => p.FAddTime).SetSize(10).Select(c => new List<object>{ c.FAage, c.FName }).Find(); //后面的 Select(columns)   方法指定了需要查询的列

#endregion
posted @ 2021-01-14 13:31  DavidChild  阅读(453)  评论(0编辑  收藏  举报