FreeSql (十五)查询数据
生活随笔
收集整理的這篇文章主要介紹了
FreeSql (十五)查询数据
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
FreeSql在查詢數據下足了功能,鏈式查詢語法、多表查詢、表達式函數支持得非常到位。
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))API
| 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 | 設置連接對象 |
系列文章導航
(一)入門
(二)自動遷移實體
(三)實體特性
(四)實體特性 Fluent Api
(五)插入數據
(六)批量插入數據
(七)插入數據時忽略列
(八)插入數據時指定列
(九)刪除數據
(十)更新數據
(十一)更新數據 Where
(十二)更新數據時指定列
(十三)更新數據時忽略列
(十四)批量更新數據
(十五)查詢數據
(十六)分頁查詢
(十七)聯表查詢
(十八)導航屬性
(十九)多表查詢
(二十)多表查詢 WhereCascade
(二十一)查詢返回數據
(二十二)Dto 映射查詢
(二十三)分組、聚合
(二十四)Linq To Sql 語法使用介紹
(二十五)延時加載
(二十六)貪婪加載 Include、IncludeMany、Dto、ToList
(二十七)將已寫好的 SQL 語句,與實體類映射進行二次查詢
(二十八)事務
(二十九)Lambda 表達式
(三十)讀寫分離
(三十一)分區分表
(三十二)Aop
(三十三)CodeFirst 類型映射
(三十四)CodeFirst 遷移說明
(三十五)CodeFirst 自定義特性
轉載于:https://www.cnblogs.com/FreeSql/p/11531339.html
總結
以上是生活随笔為你收集整理的FreeSql (十五)查询数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 七周七语言:Scala Day 3
- 下一篇: mysql 中文乱码 或 问号