FreeSql在查詢數據下足了功能,鏈式查詢語法、多表查詢、表達式函數支持得很是到位。html
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) //自動同步實體結構到數據庫 .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>();
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))
方法 | 返回值 | 參數 | 描述 |
---|---|---|---|
ToSql | string | 返回即將執行的SQL語句 | |
ToList | List
|
執行SQL查詢,返回 T1 實體全部字段的記錄,若存在導航屬性則一塊兒查詢返回,記錄不存在時返回 Count 爲 0 的列表 | |
ToList<T> | List<T> | Lambda | 執行SQL查詢,返回指定字段的記錄,記錄不存在時返回 Count 爲 0 的列表 |
ToList<T> | List<T> | string field | 執行SQL查詢,返回 field 指定字段的記錄,並以元組或基礎類型(int,string,long)接收,記錄不存在時返回 Count 爲 0 的列表 |
ToOne | T1 | 執行SQL查詢,返回 T1 實體全部字段的第一條記錄,記錄不存在時返回 null | |
Any | bool | 執行SQL查詢,是否有記錄 | |
Sum | T | Lambda | 指定一個列求和 |
Min | T | Lambda | 指定一個列求最小值 |
Max | T | Lambda | 指定一個列求最大值 |
Avg | T | Lambda | 指定一個列求平均值 |
【分頁】 | |||
Count | long | 查詢的記錄數量 | |
Count | <this> | out long | 查詢的記錄數量,以參數out形式返回 |
Skip | <this> | int offset | 查詢向後偏移行數 |
Offset | <this> | int offset | 查詢向後偏移行數 |
Limit | <this> | int limit | 查詢多少條數據 |
Take | <this> | int limit | 查詢多少條數據 |
Page | <this> | int pageIndex, int pageSize | 分頁 |
【條件】 | |||
Where | <this> | Lambda | 支持多表查詢表達式 |
WhereIf | <this> | bool, Lambda | 支持多表查詢表達式 |
Where | <this> | string, parms | 原生sql語法條件,Where("id = ?id", new { id = 1 }) |
WhereIf | <this> | bool, string, parms | 原生sql語法條件,WhereIf(true, "id = ?id", new { id = 1 }) |
WhereCascade | <this> | Lambda | 實現多表查詢時,向每一個表中附加條件 |
【分組】 | |||
GroupBy | <this> | Lambda | 按選擇的列分組,GroupBy(a => a.Name) |
GroupBy | <this> | string, parms | 按原生sql語法分組,GroupBy("concat(name, ?cc)", new { cc = 1 }) |
Having | <this> | string, parms | 按原生sql語法聚合條件過濾,Having("count(name) = ?cc", new { cc = 1 }) |
Disdinct | <this> | .Distinct().ToList(x => x.GroupName) 是對指定字段 | |
【排序】 | |||
OrderBy | <this> | Lambda | 按列排序,OrderBy(a => a.Time) |
OrderByDescending | <this> | Lambda | 按列倒向排序,OrderByDescending(a => a.Time) |
OrderBy | <this> | string, parms | 按原生sql語法排序,OrderBy("count(name) + ?cc", new { cc = 1 }) |
【聯表】 | |||
LeftJoin | <this> | Lambda | 左聯查詢,可以使用導航屬性,或指定關聯的實體類型 |
InnerJoin | <this> | Lambda | 聯接查詢,可以使用導航屬性,或指定關聯的實體類型 |
RightJoin | <this> | Lambda | 右聯查詢,可以使用導航屬性,或指定關聯的實體類型 |
LeftJoin | <this> | string, parms | 左聯查詢,使用原生sql語法,LeftJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
InnerJoin | <this> | string, parms | 聯接查詢,使用原生sql語法,InnerJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
RightJoin | <this> | string, parms | 右聯查詢,使用原生sql語法,RightJoin("type b on b.id = a.id and b.clicks > ?clicks", new { clicks = 1 }) |
From | <this> | Lambda | 多表查詢,3個表以上使用很是方便,目前設計最大支持10個表 |
【其餘】 | |||
As | <this> | string alias = "a" | 指定別名 |
Master | <this> | 指定從主庫查詢(默認查詢從庫) | |
WithTransaction | <this> | DbTransaction | 設置事務對象 |
WithConnection | <this> | DbConnection | 設置鏈接對象 |