var getAllOrder = db.Queryable<Student>() .OrderBy(it => it.Id)//asc .OrderBy(it => it.Name, OrderByType.Desc)//desc .ToList()
是否存在这条记录
var isAny = db.Queryable<Student>().Where(it => it.Id == -1).Any(); var isAny2 = db.Queryable<Student>().Any(it => it.Id == -1);
获取同一天的记录
var getTodayList = db.Queryable<Student>().Where(it => SqlFunc.DateIsSame(it.CreateTime, DateTime.Now)).ToList();
多表查询
list = db.Queryable<Student, School>((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }) .Where(st => st.Name == ).ToList(); [] { JoinType.Left,st.SchoolId==sc.Id, JoinType.Left,st.SchoolId==st2.Id }) .Where((st, sc, st2) => st2.Id == 1 || sc.Id == 1 || st.Id == 1).ToList(); list3 = db.Queryable<Student, School>((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList(); list4 = db.Queryable<Student, School>((st, sc) => new object[] { JoinType.Left,st.SchoolId==sc.Id }) .OrderBy(st => st.Id, OrderByType.Desc) .OrderBy((st, sc) => sc.Id, OrderByType.Desc) .Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = sc.Id }).ToList();
多表查询简化,与JOIN的方式高低搭配,如果不需要LEFT JOIN可以简写成下面的语句
var list5 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id)
.Select((st,sc)=>new {st.Name,st.Id,schoolName=sc.Name}).ToList();
//三表查询
var list6 = db.Queryable<Student, School,School>((st, sc,sc2) => st.SchoolId == sc.Id&&sc.Id==sc2.Id)
.Select((st, sc,sc2) => new { st.Name, st.Id, schoolName = sc.Name,schoolName2=sc2.Name }).ToList();
//三表查询分页
var list8 = db.Queryable<Student, School, School>((st, sc, sc2) => st.SchoolId == sc.Id && sc.Id == sc2.Id)
.OrderBy(st=>st.Id)
.Select((st, sc, sc2) => new { st.Name, st.Id, schoolName = sc.Name, schoolName2 = sc2.Name }).ToPageList(1, 2);
分组查询
var group = db.Queryable<Student>().GroupBy(it => it.Id) .Having(it => SqlFunc.AggregateCount(it.Id) > 10) .Select(it => new { id = SqlFunc.AggregateCount(it.Id) }).ToList();
根据字段分组取第一条,这个非常的好用
var list3 = db.Queryable<Student>() .PartitionBy(it => new { it.Id, it.Name }).Take(1).ToList();
实体转换支持2级模型
var s5 = db.Queryable<Student>().Select(it => new ViewModelStudent { Student = it, Name = it.Name }).ToList(); ViewModelStudent { public Student Student{get;set;} pulic string Name{get;set;} }
如果你是字段串拼接爱好者你也可以这样与拉姆达混用,或者纯拼SQL
, ) .AddJoinInfo(, , ) .Where() .AddParameters(new { id = 1 }) .Select().ToList();
支持的原生函数
.ToString .Contains .Length .ToLower .ToUpper .ToSubstring .Equals .HasValue .Replace .EndsWith .StartsWith .Trim
支持的自定义函数
三元判段 ,相当于 it.id==1?1:2
SqlFunc.IIF(it.Id == 1, 1, 2)
判段是NULL或者空
SqlFunc.IsNullOrEmpty(object thisValue)
判段不是NULL并且不是空
SqlFunc.HasValue(object thisValue)
判段大于0并且不等于NULL
SqlFunc.HasNumber(object thisValue)
转小写
SqlFunc.ToLower(object thisValue)
转大写
SqlFunc.ToUpper(object thisValue)
去前后空格
SqlFunc.Trim(object thisValue)
模糊查询 like %@p%
SqlFunc.Contains(string thisValue, string parameterValue)
也可以使用 .Where(it=>it.Name.Contains("a"));
In操作 thisValue={1,2,3} 生成的Sql就是 paramterValie in (1,2,3)
SqlFunc.ContainsArray(object[] thisValue, string parameterValue)
也可以使用 .Where(it=>Array.Contains(it.Id));
Not In 操作
.Where(it=>!Array.Contains(it.Id));
模糊查询 like @p%
SqlFunc.StartsWith(object thisValue, string parameterValue
模糊查询 like %@p
SqlFunc.EndsWith(object thisValue, string parameterValue)
等于
SqlFunc.Equals(object thisValue, object parameterValue)
是否是同一天
SqlFunc.DateIsSame(DateTime date1, DateTime date2)
是否是同一时间 (dataType 可以是年、月、天、小时、分钟、秒和毫秒)
SqlFunc.DateIsSame(DateTime date1, DateTime date2, DateType dataType)
在当前时间加一定时间(dataType 可以是年、月、天、小时、分钟、秒和毫秒)
SqlFunc.DateAdd(DateTime date, int addValue, DateType dataType)
在当前时间加N天
SqlFunc.DateAdd(DateTime date, int addValue)
获取当前时间的年、月、天、小时、分钟、秒或者毫秒
SqlFunc.DateValue(DateTime date, DateType dataType)
范围判段
SqlFunc.Between(object value, object start, object end)
类型转换