1.select
返回指定列匿名类集合
string sql;
var JArray1 = Newtonsoft. Json. JsonConvert. DeserializeObject < Newtonsoft. Json. Linq. JArray > ( Newtonsoft. Json. JsonConvert. SerializeObject ( db. Ado. SqlQuery < dynamic > ( sql) ) ) ;
var ILogsItems = db. Ado. SqlQuery < ILogsItem > ( sql) . ToList ( ) ;
var ILogsItem = db. Ado. SqlQuerySingle < ILogsItem > ( sql) ;
db. Ado. GetDataTable ( "select * from student" ) ;
var data = db. Queryable < Student > ( )
. Select ( f = > new
{
f. ID,
f. Name
} ) . ToList ( ) ;
排序
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID, OrderByType. Desc) . ToList ( ) ;
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID, OrderByType. Asc) . ToList ( ) ;
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID) . ToList ( ) ;
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID, OrderByType. Desc) . ToList ( ) ;
排序 多个字段
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID) . ToList ( ) ;
var getAllOrder = db. Queryable < Student > ( ) . OrderBy ( it = > it. ID, OrderByType. Desc) . ToList ( ) ;
取前 5 条
var top5 = db. Queryable < Student > ( ) . Take ( 5 ) . ToList ( ) ;
条件查询
var getByWhere = db. Queryable < Student > ( ) . Where ( it = > it. Id == 1 || it. Name == "a" ) . ToList ( ) ;
统计
root. allcount = db. Queryable < Module. DemoTest. Database. Main. Entity. Models. urlitem > ( ) . Count ( ) ;
SELECT COUNT ( * ) as 总计 FROM "main" . "T_demousers_guid"
判断是否存根据主键查询
判断记录是否存
bool exists1 = db. Queryable < Module. DemoTest. Database. Main. Entity. Models. urlitem > ( ) . Any ( it = > it. guid == aitem. hrefmd5) ;
return db. Queryable < Module. DemoTest. Database. Main. Entity. Models. urlitem > ( ) . Any ( it = > it. guid == aitem. hrefmd5) ;
随机查询出 10 条数据
urlitems = db. Queryable < urlitem> ( ) . OrderBy ( it = > SqlSugar. SqlFunc. GetRandom ( ) ) . Take ( 3 ) . ToList ( ) ;
urlitems = db. Queryable < urlitem> ( ) . Where ( it = > it. used == 0 ) . OrderBy ( it = > SqlSugar. SqlFunc. GetRandom ( ) ) . Take ( 3 ) . ToList ( ) ;
var urlitems = db. Queryable < Module. siteitem. Main. Entity. DB. Models. urlitem > ( ) . Where ( it = > it. linktype == DemoCore. Module. siteitem. Main. Const. urlitemtype. article) . OrderBy ( it = > SqlSugar. SqlFunc. GetRandom ( ) ) . Take ( 50 ) . ToList ( ) ;
特殊符号查询
SELECT T_demos. url
FROM T_demousers
WHERE T_demos. url LIKE '%/news/%'
SqlSugar 常用查询实例-拉姆达表达式
1. 查询列表
SqlSugarClient db = SugarContext. GetInstance ( ) ;
List< teacher> tList = db. Queryable < teacher> ( ) . ToList ( ) ;
Console. WriteLine ( tList. Count) ;
var studentDynamic = db. Queryable < student> ( ) . ToDynamic ( ) ;
Console. WriteLine ( studentDynamic) ;
string teaJson = db. Queryable < teacher> ( ) . ToJson ( ) ;
Console. WriteLine ( teaJson) ;
db. SerializerDateFormat = "yyyy年MM月dd日" ;
string teacher1 = db. Queryable < teacher> ( ) . OrderBy ( "tbirthday desc" ) . Take ( 2 ) . ToJson ( ) ;
string teacher2 = db. Sqlable ( ) . From < teacher> ( "t" ) . SelectToJson ( " top 1 *" ) ;
string teacher3 = db. SqlQueryJson ( "select top 1 * from teacher;" ) ;
Console. WriteLine ( teacher1) ;
Console. WriteLine ( teacher2) ;
Console. WriteLine ( teacher3) ;
Console. WriteLine ( tList. First ( ) . tbirthday. Value. ToString ( "yyyy-MM-dd" ) ) ;
2. 常用查询
SqlSugarClient db = SugarContext. GetInstance ( ) ;
teacher tea1 = db. Queryable < teacher> ( ) . Single ( t = > t. tno == "804" ) ;
Console. WriteLine ( tea1. tname) ;
teacher tea2 = db. Queryable < teacher> ( ) . InSingle ( "814" ) ;
Console. WriteLine ( tea2. ToJsonString ( ) ) ;
teacher tea3 = db. Queryable < teacher> ( ) . SingleOrDefault ( t = > t. tno == "814" ) ;
Console. WriteLine ( tea3. ToJsonString ( ) ) ;
teacher tea4 = db. Queryable < teacher> ( )
. Where ( q = > q. tno == "814" )
. FirstOrDefault ( ) ;
Console. WriteLine ( tea4. ToJsonString ( ) ) ;
bool exists1 = db. Queryable < teacher> ( ) . Any ( q = > q. tno == "814" ) ;
Console. WriteLine ( exists1) ;
List< teacher> contains1 = db. Queryable < teacher> ( )
. Where ( q = > q. tname. Contains ( "李" ) )
. ToList ( ) ;
Console. WriteLine ( contains1. ToJsonString ( ) ) ;
List< teacher> end1 = db. Queryable < teacher> ( )
. Where ( q = > q. tname. EndsWith ( "成" ) )
. ToList ( ) ;
Console. WriteLine ( end1. ToJsonString ( ) ) ;
string [ ] idList = new string [ ] { "804" , "825" } ;
List< teacher> in1 = db. Queryable < teacher> ( )
. In ( q = > q. tno, idList)
. ToList ( ) ;
Console. WriteLine ( in1. ToJsonString ( ) ) ;
List< teacher> in2 = db. Queryable < teacher> ( )
. Where ( q = > idList. Contains ( q. tno) )
. ToList ( ) ;
Console. WriteLine ( in2. ToJsonString ( ) ) ;
int count = db. Queryable < teacher> ( )
. Where ( q = > q. tsex == "男" )
. Count ( ) ;
Console. WriteLine ( "count=" + count) ;
decimal maxDegree = db. Queryable < score> ( )
. Where ( q = > q. cno == "3-105" )
. Max ( q = > q. degree)
. ObjToDecimal ( ) ;
Console. WriteLine ( "maxDegree:" + maxDegree) ;
decimal minDegree = db. Queryable < score> ( )
. Where ( q = > q. cno == "3-105" )
. Min ( q = > q. degree)
. ObjToDecimal ( ) ;
Console. WriteLine ( "minDegree:" + minDegree) ;
3. 分页查询
SqlSugarClient db = SugarContext. GetInstance ( ) ;
Queryable< teacher> query = db. Queryable < teacher> ( ) ;
query = query. Where ( q = > q. tno. StartsWith ( "8" ) ) ;
query = query. OrderBy ( q = > q. tbirthday) ;
int allCount = query. Count ( ) ;
List< teacher> list1 = query. ToPageList ( 1 , 2 ) ;
Console. WriteLine ( "list1-count:" + allCount) ;
Console. WriteLine ( "list1:" + list1. ToJsonString ( ) ) ;
Queryable< student> query2 = db. Queryable < student> ( ) ;
query2 = query2. Where ( q = > q. ssex == "男" ) ;
query2 = query2. Where ( q = > q. sname. EndsWith ( "军" ) ) ;
query2 = query2. OrderBy ( q = > q. sbirthday) ;
int allCount2 = 0 ;
List< student> list2 = query2. ToPageList ( 1 , 2 , ref allCount2) ;
Console. WriteLine ( "list2-count:" + allCount2) ;
Console. WriteLine ( "list2:" + list2. ToJsonString ( ) ) ;
4. 汇总查询 2
SqlSugarClient db = SugarContext. GetInstance ( ) ;
dynamic result1 = db. Queryable < student> ( )
. GroupBy ( q = > q. ssex)
. Select ( "ssex,count(*) Count" )
. ToDynamic ( ) ;
foreach ( var item in result1)
{
Console. WriteLine ( item. ssex + "----" + item. Count) ;
}
string result2 = db. Queryable < score> ( )
. GroupBy ( q = > q. sno)
. Select ( "sno,isnull(sum(degree),0)" )
. ToJson ( ) ;
Console. WriteLine ( result2) ;
5. 关联表查询
static void QueryFive ( )
{
SqlSugarClient db = SugarContext. GetInstance ( ) ;
var result = db. Queryable < score> ( )
. JoinTable < student> ( ( s1, s2) = > s1. sno == s2. sno)
. OrderBy ( s1 = > s1. degree)
. Take ( 1 )
. Select < score, student, Score_Student > ( ( s1, s2) = > new Score_Student
{
sno = s1. sno,
sname = s2. sname,
cno = s1. cno,
degree = s1. degree
} ) . ToList ( ) ;
Console. WriteLine ( result. ToJsonString ( ) ) ;
var result2 = db. Queryable < score> ( )
. JoinTable < student> ( ( s1, s2) = > s1. sno == s2. sno)
. Where ( s1 = > s1. degree > 64 )
. Where ( s1 = > s1. degree < 70 )
. ToJson ( ) ;
Console. WriteLine ( result2) ;
var result3 = db. Queryable < student> ( )
. OrderBy ( q = > q. sbirthday)
. Take ( 1 )
. ToList ( )
. Select ( q = > new
{
sno = q. sno,
sname = q. sname
} ) ;
Console. WriteLine ( result3. ToJsonString ( ) ) ;
var result4 = db. Queryable < score> ( )
. JoinTable < student> ( ( s1, s2) = > s1. sno == s2. sno)
. OrderBy ( s1 = > s1. degree)
. Take ( 1 )
. Select ( "s1.*,s2.sname as sname" )
. ToJson ( ) ;
Console. WriteLine ( result4) ;
}
public class Score_Student
{
public string sno { get ; set ; }
public string sname { get ; set ; }
public decimal degree { get ; set ; }
public string cno { get ; set ; }
}
6. Between 查询 尝试时间查询失败记录
bool exists2 = db. Queryable < demo> ( ) . Any ( it = > it. UniqueId == apirequestpackagetaskqueueinstanttask. websiteNewsUpdate. host && SqlFunc. Between ( it. createtime, DateTime. Now. AddMinutes ( - 1 ) , DateTime. Now) ) ;
7. 根据主键查询
var getByPrimaryKey = db. Queryable < Student > ( ) . InSingle ( 2 ) ;
8. 是否存在这条记录
var isAny = db. Queryable < Student > ( ) . Where ( it = > it. Id == - 1 ) . Any ( ) ;
var isAny2 = db. Queryable < Student > ( ) . Any ( it = > it. Id == - 1 ) ;
9. 查询返回值
var isAny = db. Queryable < Student > ( ) . Where ( it = > it. Id == - 1 ) . Any ( ) ;
var isAny2 = db. Queryable < Student > ( ) . Any ( it = > it. Id == - 1 ) ;
1. 返回指定类集合
List< ResultModel> data = db. Queryable < Student > ( )
. Select ( f = > new ResultModel
{
ID = f. ID,
StudentName = f. Name
} ) . ToList ( ) ;
2. 使用字符串代替 Lambda 表达进行查询
3. 只查询某列
List< Student> list = db. Queryable < Student > ( ) . Select ( "guid" ) . ToList ( ) ;
List< Student> list = db. Queryable < Student > ( ) . Select ( "*" ) . ToList ( ) ;
List< ResultModel> list = db. Queryable < Student > ( ) . Select < ResultModel > ( "ID, Name" ) . ToList ( ) ;
4. 查询单条
var getSingleOrDefault = db. Queryable < Student > ( ) . Single ( ) ;
var getFirstOrDefault = db. Queryable < Student > ( ) . First ( ) ;
10. 执行 SQL
卡: 随机值不要sql里做:
Students = db. SqlQueryable < Student > ( "SELECT * FROM Student LIMIT 1 OFFSET ABS(RANDOM()) % MAX((SELECT COUNT(*) FROM Student), 1)" ) . ToList ( ) ;
分布操作获取偏移值:
SELECT * FROM T_demousers WHERE T_demos. used = 0 LIMIT 1 OFFSET r
SELECT * FROM T_demousers WHERE T_demos. used = 0 LIMIT 1 OFFSET 2
SELECT * FROM T_demousers WHERE T_demos. used = 0 LIMIT 1 OFFSET 3
var t12 = db. SqlQueryable < Student > ( "select * from student" ) . Where ( it= > it. id> 0 ) . ToPageList ( 1 , 2 ) ;
var t12 = db. SqlQueryable < dynamic > ( "select * from student" ) . ToPageList ( 1 , 2 ) ;
var t12 = db. SqlQueryable < Student > ( Sql) . Take ( Amount) . ToList ( ) ;
var t12 = db. SqlQueryable < dynamic > ( "SELECT COUNT (guid) as cnt FROM \"main\".\"T_demousers\"" ) . ToPageList ( 1 , 2 ) ;
root. allcount = Convert. ToInt32 ( t12[ 0 ] . cnt) ;
1. Like
SELECT * FROM T_demousers_guid WHERE T_demousers_guid. url LIKE '%163.com%' AND
T_demousers_guid. url LIKE '%.html%' AND
T_demousers_guid. url LIKE '%/%'
2.Delete
代码删除
demo 一
using ( var db = GetInstance_main_log ( ) )
{
var count = db. Deleteable < logitem> ( ) . ExecuteCommand ( ) ;
db. Ado. GetInt ( "VACUUM" ) ;
root. code = 200 ;
root. message = string . Format ( "删除成功!删除数据总量:{0}" , count) ;
}
Official
根据实体删除(实体内主键一定要有值)
var t0 = db. Deleteable < Student > ( ) . Where ( new Student ( ) { Id = 1 } ) . ExecuteCommand ( ) ;
根据实体集删除
var t1 = db. Deleteable < Student > ( ) . Where ( new List < Student > ( ) { new Student ( ) { Id = 1 } } ) . ExecuteCommand ( ) ;
使用锁
var t2 = db. Deleteable < Student > ( ) . With ( SqlWith. RowLock) . ExecuteCommand ( ) ;
根据主键删除
var t3 = db. Deleteable < Student > ( ) . In ( 1 ) . ExecuteCommand ( ) ;
根据主键批量删除
var t4 = db. Deleteable < Student > ( ) . In ( new int [ ] { 1 , 2 } ) . ExecuteCommand ( ) ;
根据非主键批量删除4.9
var t4 = db. Deleteable < Student > ( ) . In ( it= > it. SchoolId, new int [ ] { 1 , 2 } ) . ExecuteCommand ( ) ;
根据表达式删除
var t5 = db. Deleteable < Student > ( ) . Where ( it = > it. Id == 1 ) . ExecuteCommand ( ) ;
list< int > list= new list < int > ( ) { 1 , 3 } ;
var t5 = db. Deleteable < Student > ( ) . Where ( it = > ! list. Contains ( it. Id) ) . ExecuteCommand ( ) ;
4.1 .0 .6 版本之后可以对上面的语法进行优化
db. Deleteable < Student > ( 1 ) . ExecuteCommand ( ) ;
db. Deleteable < Student > ( it= > it. id== 1 ) . ExecuteCommand ( ) ;
db. Deleteable < Student > ( new int [ ] { 1 , 2 , 3 } ) . ExecuteCommand ( ) ;
db. Deleteable < Student > ( 实体) . ExecuteCommand ( ) ;
3.分页查询
public static bool ReleaseResources ( )
{
Thread oThread = new Thread ( delegate ( )
{
System. Diagnostics. Stopwatch stopwatch = new System. Diagnostics. Stopwatch ( ) ;
stopwatch. Reset ( ) ;
stopwatch. Start ( ) ;
var pageIndex = 1 ;
var pageSize = 1000 ;
var totalCount = 0 ;
var totalPage = 1 ;
object lockObj_lock = new object ( ) ;
var SuccessTotalCount = 0 ;
using ( var db = GetInstance_main_urlitems ( ) )
{
var urlitems = db. Queryable < urlitem> ( ) . ToPageList ( pageIndex, pageSize, ref totalCount, ref totalPage) ;
}
Parallel. For ( 0 , totalPage, new ParallelOptions ( ) { MaxDegreeOfParallelism = 60 } , ( i, loopState) = >
{
using ( var db = GetInstance_main_urlitems ( ) )
{
var urlitems = db. Queryable < urlitem> ( ) . Select ( "guid" ) . ToPageList ( i+ 1 , pageSize) ;
lock ( lockObj_lock)
{
SuccessTotalCount + = urlitems. Count;
}
}
} ) ;
stopwatch. Stop ( ) ;
Console. WriteLine ( "总数据量:{0}" , totalCount) ;
Console. WriteLine ( "成功遍历{0}条数据 ,用时{1}秒。" , SuccessTotalCount, stopwatch. Elapsed. TotalSeconds) ;
Console. WriteLine ( "总页数:{0}" , totalPage) ;
} ) ;
oThread. IsBackground = true ;
oThread. Start ( ) ;
return true ;
}