FreeSql在查詢數(shù)據(jù)下足了功能,鏈?zhǔn)讲樵冋Z(yǔ)法、多表查詢、表達(dá)式函數(shù)支持得非常到位。
IFreeSql fsql = new FreeSql.FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, "Data
Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial
Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10")
.UseAutoSyncStructure(true) //自動(dòng)同步實(shí)體結(jié)構(gòu)到數(shù)據(jù)庫(kù) .Build(); [Table(Name = "tb_topic")]
class Topic { [Column(IsIdentity = true, IsPrimary = true)] public int Id {
get; set; } public int Clicks { get; set; } public int TestTypeInfoGuid { get;
set; } public string Title { get; set; } public DateTime CreateTime { get; set;
} } ISelect<Topic> select => fsql.Select<Topic>();
查詢數(shù)據(jù)
var sql = select.Where(a => a.Id == 10).ToSql(); ///SELECT a.`Id`, a.`Clicks`,
a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` //FROM `tb_topic` a //WHERE
(a.`Id` = 10) sql = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks >
100).ToSql(); ///SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`,
a.`CreateTime` //FROM `tb_topic` a //WHERE (a.`Id` = 10 AND a.`Id` > 10 OR
a.`Clicks` > 100) sql = select.Where(a => new
[]{1,2,3}.Contains(a.Id)).ToSql(); //SELECT a.`Id`, a.`Clicks`,
a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` //FROM `tb_topic` a //WHERE
(a.`Id` in (1,2,3))
API
方法 返回值 參數(shù) 描述
ToSql string 返回即將執(zhí)行的SQL語(yǔ)句
ToList List 執(zhí)行SQL查詢,返回 T1 實(shí)體所有字段的記錄,若存在導(dǎo)航屬性則一起查詢返回,記錄不存在時(shí)返回 Count 為 0 的列表
ToList<T> List<T> Lambda 執(zhí)行SQL查詢,返回指定字段的記錄,記錄不存在時(shí)返回 Count 為 0 的列表
ToList<T> List<T> string field 執(zhí)行SQL查詢,返回 field
指定字段的記錄,并以元組或基礎(chǔ)類型(int,string,long)接收,記錄不存在時(shí)返回 Count 為 0 的列表
ToOne T1 執(zhí)行SQL查詢,返回 T1 實(shí)體所有字段的第一條記錄,記錄不存在時(shí)返回 null
Any bool 執(zhí)行SQL查詢,是否有記錄
Sum T Lambda 指定一個(gè)列求和
Min T Lambda 指定一個(gè)列求最小值
Max T Lambda 指定一個(gè)列求最大值
Avg T Lambda 指定一個(gè)列求平均值
【分頁(yè)】
Count long 查詢的記錄數(shù)量
Count <this> out long 查詢的記錄數(shù)量,以參數(shù)out形式返回
Skip <this> int offset 查詢向后偏移行數(shù)
Offset <this> int offset 查詢向后偏移行數(shù)
Limit <this> int limit 查詢多少條數(shù)據(jù)
Take <this> int limit 查詢多少條數(shù)據(jù)
Page <this> int pageIndex, int pageSize 分頁(yè)
【條件】
Where <this> Lambda 支持多表查詢表達(dá)式
WhereIf <this> bool, Lambda 支持多表查詢表達(dá)式
Where <this> string, parms 原生sql語(yǔ)法條件,Where("id = ?id", new { id = 1 })
WhereIf <this> bool, string, parms 原生sql語(yǔ)法條件,WhereIf(true, "id = ?id", new {
id = 1 })
WhereCascade <this> Lambda 實(shí)現(xiàn)多表查詢時(shí),向每個(gè)表中附加條件
【分組】
GroupBy <this> Lambda 按選擇的列分組,GroupBy(a => a.Name)
GroupBy <this> string, parms 按原生sql語(yǔ)法分組,GroupBy("concat(name, ?cc)", new { cc
= 1 })
Having <this> string, parms 按原生sql語(yǔ)法聚合條件過(guò)濾,Having("count(name) = ?cc", new {
cc = 1 })
Disdinct <this> .Distinct().ToList(x => x.GroupName) 是對(duì)指定字段
【排序】
OrderBy <this> Lambda 按列排序,OrderBy(a => a.Time)
OrderByDescending <this> Lambda 按列倒向排序,OrderByDescending(a => a.Time)
OrderBy <this> string, parms 按原生sql語(yǔ)法排序,OrderBy("count(name) + ?cc", new { cc
= 1 })
【聯(lián)表】
LeftJoin <this> Lambda 左聯(lián)查詢,可使用導(dǎo)航屬性,或指定關(guān)聯(lián)的實(shí)體類型
InnerJoin <this> Lambda 聯(lián)接查詢,可使用導(dǎo)航屬性,或指定關(guān)聯(lián)的實(shí)體類型
RightJoin <this> Lambda 右聯(lián)查詢,可使用導(dǎo)航屬性,或指定關(guān)聯(lián)的實(shí)體類型
LeftJoin <this> string, parms 左聯(lián)查詢,使用原生sql語(yǔ)法,LeftJoin("type b on b.id = a.id
and b.clicks > ?clicks", new { clicks = 1 })
InnerJoin <this> string, parms 聯(lián)接查詢,使用原生sql語(yǔ)法,InnerJoin("type b on b.id = a.id
and b.clicks > ?clicks", new { clicks = 1 })
RightJoin <this> string, parms 右聯(lián)查詢,使用原生sql語(yǔ)法,RightJoin("type b on b.id = a.id
and b.clicks > ?clicks", new { clicks = 1 })
From <this> Lambda 多表查詢,3個(gè)表以上使用非常方便,目前設(shè)計(jì)最大支持10個(gè)表
【其他】
As <this> string alias = "a" 指定別名
Master <this> 指定從主庫(kù)查詢(默認(rèn)查詢從庫(kù))
WithTransaction <this> DbTransaction 設(shè)置事務(wù)對(duì)象
WithConnection <this> DbConnection 設(shè)置連接對(duì)象
系列文章導(dǎo)航
*
(一)入門 <https://www.cnblogs.com/FreeSql/p/11531300.html>
*
(二)自動(dòng)遷移實(shí)體 <https://www.cnblogs.com/FreeSql/p/11531301.html>
*
(三)實(shí)體特性 <https://www.cnblogs.com/FreeSql/p/11531302.html>
*
(四)實(shí)體特性 Fluent Api <https://www.cnblogs.com/FreeSql/p/11531304.html>
*
(五)插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531306.html>
*
(六)批量插入數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531309.html>
*
(七)插入數(shù)據(jù)時(shí)忽略列 <https://www.cnblogs.com/FreeSql/p/11531316.html>
*
(八)插入數(shù)據(jù)時(shí)指定列 <https://www.cnblogs.com/FreeSql/p/11531318.html>
*
(九)刪除數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531320.html>
*
(十)更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531321.html>
*
(十一)更新數(shù)據(jù) Where <https://www.cnblogs.com/FreeSql/p/11531324.html>
*
(十二)更新數(shù)據(jù)時(shí)指定列 <https://www.cnblogs.com/FreeSql/p/11531327.html>
*
(十三)更新數(shù)據(jù)時(shí)忽略列 <https://www.cnblogs.com/FreeSql/p/11531334.html>
*
(十四)批量更新數(shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531335.html>
*
(十五)查詢數(shù)據(jù)
*
(十六)分頁(yè)查詢 <https://www.cnblogs.com/FreeSql/p/11531341.html>
*
(十七)聯(lián)表查詢 <https://www.cnblogs.com/FreeSql/p/11531346.html>
*
(十八)導(dǎo)航屬性 <https://www.cnblogs.com/FreeSql/p/11531352.html>
*
(十九)多表查詢 <https://www.cnblogs.com/FreeSql/p/11531362.html>
*
(二十)多表查詢 WhereCascade <https://www.cnblogs.com/FreeSql/p/11531372.html>
*
(二十一)查詢返回?cái)?shù)據(jù) <https://www.cnblogs.com/FreeSql/p/11531376.html>
*
(二十二)Dto 映射查詢 <https://www.cnblogs.com/FreeSql/p/11531381.html>
*
(二十三)分組、聚合 <https://www.cnblogs.com/FreeSql/p/11531384.html>
*
(二十四)Linq To Sql 語(yǔ)法使用介紹 <https://www.cnblogs.com/FreeSql/p/11531392.html>
*
(二十五)延時(shí)加載 <https://www.cnblogs.com/FreeSql/p/11531395.html>
*
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
<https://www.cnblogs.com/FreeSql/p/11531404.html>
*
(二十七)將已寫(xiě)好的 SQL 語(yǔ)句,與實(shí)體類映射進(jìn)行二次查詢
<https://www.cnblogs.com/FreeSql/p/11531416.html>
*
(二十八)事務(wù) <https://www.cnblogs.com/FreeSql/p/11531423.html>
*
(二十九)Lambda 表達(dá)式 <https://www.cnblogs.com/FreeSql/p/11531425.html>
*
(三十)讀寫(xiě)分離 <https://www.cnblogs.com/FreeSql/p/11531430.html>
*
(三十一)分區(qū)分表 <https://www.cnblogs.com/FreeSql/p/11531435.html>
*
(三十二)Aop <https://www.cnblogs.com/FreeSql/p/11531471.html>
*
(三十三)CodeFirst 類型映射 <https://www.cnblogs.com/FreeSql/p/11531543.html>
*
(三十四)CodeFirst 遷移說(shuō)明 <https://www.cnblogs.com/FreeSql/p/11531550.html>
*
(三十五)CodeFirst 自定義特性 <https://www.cnblogs.com/FreeSql/p/11531576.html>
熱門工具 換一換
