var test = db.Queryable().Where(it => it.C_BOB != null).ToList();
//查询所有
var student = db.Queryable().ToList();
var studentDynamic = db.Queryable().ToDynamic();
var studentJson = db.Queryable().ToJson();
//查询单条
var single = db.Queryable().Single(c => c.id == 1);
//查询单条根据主键
var singleByPk = db.Queryable().InSingle(1);
//查询单条没有记录返回空对象
var singleOrDefault = db.Queryable().SingleOrDefault(c => c.id == 11111111);
//查询单条没有记录返回空对象
var single2 = db.Queryable().Where(c => c.id == 1).SingleOrDefault();
//查询第一条
var first = db.Queryable().Where(c => c.id == 1).First();
var first2 = db.Queryable().Where(c => c.id == 1).FirstOrDefault();
//取11-20条
var page1 = db.Queryable().Where(c => c.id > 10).OrderBy(it => it.id).Skip(10).Take(10).ToList();
//取11-20条 等于 Skip(pageIndex-1)*pageSize).Take(pageSize) 等于 between (pageIndex-1)pageSize and pageIndexpageSize
var page2 = db.Queryable().Where(c => c.id > 10).OrderBy(it => it.id).ToPageList(2, 10);
//查询条数
var count = db.Queryable().Where(c => c.id > 10).Count();
//从第2条开始以后取所有
var skip = db.Queryable().Where(c => c.id > 10).OrderBy(it => it.id).Skip(2).ToList();
//取前2条
var take = db.Queryable().Where(c => c.id > 10).OrderBy(it => it.id).Take(2).ToList();
//Not like
string conval = "a";
var notLike = db.Queryable().Where(c => !c.name.Contains(conval.ToString())).ToList();
//Like
conval = "三";
var like = db.Queryable().Where(c => c.name.Contains(conval)).ToList();
//支持字符串Where 让你解决,更复杂的查询
var student12 = db.Queryable().Where(c => "a" == "a").Where("id>:id", new { id = 1 }).ToList();
var student13 = db.Queryable().Where(c => "a" == "a").Where("id>100 and id in( select 1 from dual )").ToList();
//存在记录反回true,则否返回false
bool isAny100 = db.Queryable().Any(c => c.id == 100);
bool isAny1 = db.Queryable().Any(c => c.id == 1);
//获取最大Id
object maxId = db.Queryable().Max(it => it.id);
int maxId1 = db.Queryable().Max(it => it.id).ObjToInt();//拉姆达
int maxId2 = db.Queryable().Max("id"); //字符串写法
//获取最小
int minId1 = db.Queryable().Where(c => c.id > 0).Min(it => it.id).ObjToInt();//拉姆达
int minId2 = db.Queryable().Where(c => c.id > 0).Min("id");//字符串写法
//order By
var orderList = db.Queryable().OrderBy("id desc,name asc").ToList();//字符串支持多个排序
//可以多个order by表达示
var order2List = db.Queryable().OrderBy(it => it.name).OrderBy(it => it.id, OrderByType.desc).ToList(); // order by name as ,order by id desc
//In
var intArray = new[] { "5", "2", "3" };
var intList = intArray.ToList();
var listnew = db.Queryable().Where(it => intArray.Contains(it.name)).ToList();
var list0 = db.Queryable().In(it => it.id, 1, 2, 3).ToList();
var list1 = db.Queryable().In(it => it.id, intArray).ToList();
var list2 = db.Queryable().In("id", intArray).ToList();
var list3 = db.Queryable().In(it => it.id, intList).ToList();
var list4 = db.Queryable().In("id", intList).ToList();
var list6 = db.Queryable().In(intList).ToList();//不设置字段默认主键
//分组查询
var list7 = db.Queryable().Where(c => c.id < 20).GroupBy(it => it.sex).Select("sex,count() Count").ToDynamic();
var list8 = db.Queryable().Where(c => c.id < 20).GroupBy(it => it.sex).GroupBy(it => it.id).Select("id,sex,count() Count").ToDynamic();
List list9 = db.Queryable().Where(c => c.id < 20).GroupBy(it => it.sex).Select("Sex,count() Count").ToList();
List list10 = db.Queryable().Where(c => c.id < 20).GroupBy("sex").Select("Sex,count() Count").ToList();
//SELECT Sex,Count=count(*) FROM Student WHERE 1=1 AND (id < 20) GROUP BY Sex --生成结果
//2表关联查询
var jList = db.Queryable()
.JoinTable((s1, s2) => s1.sch_id == s2.id) //默认left join
.Where((s1, s2) => s1.id == 1)
.Select("s1.*,s2.name as schName")
.ToDynamic();
/等于同于
SELECT s1.,s2.name as schName
FROM [Student] s1
LEFT JOIN [School] s2 ON s1.sch_id = s2.id
WHERE s1.id = 1 */
//2表关联查询并分页
var jList2 = db.Queryable()
.JoinTable((s1, s2) => s1.sch_id == s2.id) //默认left join
//如果要用inner join这么写
//.JoinTable((s1, s2) => s1.sch_id == s2.id ,JoinType.INNER)
.Where((s1, s2) => s1.id > 1)
.OrderBy(s1 => s1.name)
.Skip(10)
.Take(20)
.Select("s1.*,s2.name as schName")
.ToDynamic();
//3表查询并分页
var jList3 = db.Queryable()
.JoinTable((s1, s2) => s1.sch_id == s2.id) // left join School s2 on s1.id=s2.id
.JoinTable((s1, s3) => s1.sch_id == s3.id) // left join School s3 on s1.id=s3.id
.Where((s1, s2) => s1.id > 1) // where s1.id>1
.Where(s1 => s1.id > 0)
.OrderBy((s1, s2) => s1.id) //order by s1.id 多个order可以 .oderBy().orderby 叠加
.Skip(10)
.Take(20)
.Select("s1.*,s2.name as schName,s3.name as schName2")//select目前只支持这种写法
.ToDynamic();
//上面的方式都是与第一张表join,第三张表想与第二张表join写法如下
List jList4 =
db.Queryable()
.JoinTable((s1, s2) => s1.sch_id == s2.id) // left join School s2 on s1.id=s2.id
.JoinTable((s1, s2, a1) => a1.id == s2.AreaId)// left join Area a1 on a1.id=s2.AreaId 第三张表与第二张表关联
.JoinTable((s1, a1, s3) => a1.id == s3.AreaId)// left join School s3 on a1.id=s3.AreaId 第四第表第三张表关联
.JoinTable((s1, s4) => s1.sch_id == s4.id) // left join School s2 on s1.id=s4.id
.Select((s1, s2, a1) => new V_Student { id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name }).ToList();
//等同于
//SELECT id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name
//FROM [Student] s1
//LEFT JOIN School s2 ON ( s1.sch_id = s2.id )
//LEFT JOIN Area a1 ON ( a1.id = s2.AreaId ) //第三张表与第二张表关联
//LEFT JOIN School s3 ON ( a1.id = s3.AreaId ) //第四张表与第三张表关联
//LEFT JOIN School s4 ON ( s1.sch_id = s4.id )
//WHERE 1=1
//Join子查询语句加分页的写法
var childQuery = db.Queryable().Where("id=:id").Select(it => new { id = it.id }).ToSql();//创建子查询SQL
string childTableName =SqlSugarTool.PackagingSQL(childQuery.Key);//将SQL语句用()包成表
var queryable = db.Queryable()
.JoinTable((s1, s2) => s1.sch_id == s2.id) //LEFT JOIN School s2 ON ( s1.sch_id = s2.id )
.JoinTable(childTableName, "a1", "a1.id=s2.areaid", new { id = 1 }, JoinType.INNER) //INNER JOIN (SELECT * FROM [Area] WHERE 1=1 AND id=@id ) a1 ON a1.id=s2.areaid
.OrderBy(s1 => s1.id);
var list = queryable.Select((s1, s2, a1) => new V_Student { id = s1.id, name = s1.name, SchoolName = s2.name, AreaName = a1.name })
.ToPageList(0, 200);
var count2 = queryable.Count();
//拼接例子
var queryable2 = db.Queryable().Where(it => true);
if (maxId.ObjToInt() == 1)
{
queryable2.Where(it => it.id == 1);
}
else
{
queryable2.Where(it => it.id == 2);
}
var listJoin = queryable2.ToList();
//queryable和SqlSugarClient解耦
var par = new Queryable().Where(it => it.id == 1);//声名没有connection对象的Queryable
par.DB = db;
var listPar = par.ToList();
//查看生成的sql和参数
var id = 1;
var sqlAndPars = db.Queryable().Where(it => it.id == id).OrderBy(it => it.id).ToSql();