Bitter.Core系列四:Bitter ORM NETCORE ORM 全网最粗暴简单易用高性能的 NETCore ORM 之 示例 查询
2024-09-08 05:19:54
一: 单表模型驱动查询 如下示例代码演示:
// 根据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
最新文章
- Centos 7 minimal install 无网络无ifconfig的解决
- js事件模型与自定义事件
- js undefine,null 和NaN
- 两个oracle之间建立db link
- java中产生对象的两种方式
- C#并行编程--命令式数据并行(Parallel.Invoke)---与匿名函数一起理解(转载整理)
- 模拟游客一天的生活与旅游java程序代写源码
- javscript面试题(一)
- 大白话Vue源码系列目录
- Android studio签名与代码混淆
- 一键安装Cloud Torrent
- TestNG Suite 运行出现中文乱码如何解决
- springboot+thymeleaf+springbootJPA实现一个简单的增删改查
- bayer格式
- django CBV和FBV写法总结
- Guava CharMatcher
- [Timer]应用层实现sleep
- struts2 自带的 token防止表单重复提交拦截器
- Jquery 对比 Javascript
- php实现图形计算器