MyDAL - like && not like 條件 使用
索引:
一.API 列表
C# 程式碼中String.Contains("conditionStr") 生成 SQL 對應的like '%conditionStr%'
如:.Queryer<Agent>()
... ...
.Where(it => it.PathId.Contains("~00-d-3-1-"))
... ... 用於單表 like 條件
.Queryer(out Agent agent1, out AgentInventoryRecord record1)
... ...
.Where(() => agent1.Name.Contains("陳"))
... ... 用於多表連線 like 條件
C# 程式碼中String.StartsWith("conditionStr") 生成 SQL 對應的like 'conditionStr%'
如:.Queryer<Agent>()
... ...
.Where(it => it.PathId.StartsWith("~00-d-3-1-"))
... ... 用於單表 like 條件
.Queryer(out Agent agent13, out AgentInventoryRecord record13)
... ...
.Where(() => agent13.Name.StartsWith("張"))
... ... 用於多表連線 like 條件
C# 程式碼中String.EndsWith("conditionStr") 生成 SQL 對應的like '%conditionStr'
如:.Queryer<Agent>()
... ...
.Where(it => it.PathId.EndsWith("~00-d-3-1-"))
... ... 用於單表 like 條件
.Queryer(out Agent agent13, out AgentInventoryRecord record13)
... ...
.Where(() => agent13.Name.EndsWith("華"))
... ... 用於多表連線 like 條件
MySQL 萬用字元% (百分號) / _ (下劃線)
在 string 變數中若檢測到 萬用字元 存在,則以自定義的萬用字元表示式 在 DB 中進行 like 查詢
C# 程式碼中 萬用字元轉義/% (百分號轉義) / /_ (下劃線轉義)
在 string 變數中若檢測到 萬用字元轉義 存在 ,則會在 DB 中以轉義後 字面值 的形式進行 like 查詢
二.API 單表-便捷 方法 舉例
1. like 條件
1 var res1 = await Conn.QueryListAsync<Agent>(it => it.Name.Contains("陳"));
生成 SQL 如下
1 select * 2 from `agent` 3 where`Name` likeCONCAT('%',@Name__1,'%');
2. not like 條件
1 var res1 = await Conn.QueryListAsync<Agent>(it => !it.Name.Contains("劉"));
生成 SQL 如下
1 select * 2 from `agent` 3 where`Name` not likeCONCAT('%',@Name__1,'%');
三.API 單表-完整 方法 舉例
1. like 條件
1var res1 = await Conn 2.Queryer<Agent>() 3.Where(it => it.CreatedOn >= Convert.ToDateTime("2018-08-23 13:36:58").AddDays(-30)) 4.And(it => it.PathId.Contains("~00-d-3-1-")) 5.PagingListAsync(1, 10);
生成 SQL 如下
1 -- 總數 2 selectcount(*) 3 from `agent` 4 where`CreatedOn`>=@CreatedOn__1 5and`PathId` likeCONCAT('%',@PathId__2,'%'); 6 7 -- 分頁資料 8 select * 9 from `agent` 10 where`CreatedOn`>=@CreatedOn__1 11and`PathId` likeCONCAT('%',@PathId__2,'%') 12 order by `Id` desc 13 limit 0,10;
2. not like 條件
1var res1 = await Conn 2.Queryer<Agent>() 3.Where(it => !it.PathId.Contains("~00-d-3-1-")) 4.PagingListAsync(1, 10);
生成 SQL 如下
1 -- 總數 2 selectcount(*) 3 from `agent` 4 where`PathId` not likeCONCAT('%',@PathId__1,'%'); 5 6 -- 分頁資料 7 select * 8 from `agent` 9 where`PathId` not likeCONCAT('%',@PathId__1,'%') 10 order by `Id` desc 11 limit 0,10;
四.API 多表連線-完整 方法 舉例
1. like 條件
1var res1 = await Conn 2.Queryer(out Agent agent1, out AgentInventoryRecord record1) 3.From(() => agent1) 4.InnerJoin(() => record1) 5.On(() => agent1.Id == record1.AgentId) 6.Where(() => agent1.Name.Contains("陳")) 7.QueryListAsync<AgentInventoryRecord>();
生成 SQL 如下
1 select record1.`*` 2 from `agent` as agent1 3inner join `agentinventoryrecord` as record1 4on agent1.`Id`=record1.`AgentId` 5 whereagent1.`Name` likeCONCAT('%',@Name__4,'%');
2. not like 條件
1var res1 = await Conn 2.Queryer(out Agent agent1, out AgentInventoryRecord record1) 3.From(() => agent1) 4.InnerJoin(() => record1) 5.On(() => agent1.Id == record1.AgentId) 6.Where(() => !agent1.Name.Contains("陳")) 7.QueryListAsync<AgentInventoryRecord>();
生成 SQL 如下
1 select record1.`*` 2 from `agent` as agent1 3inner join `agentinventoryrecord` as record1 4on agent1.`Id`=record1.`AgentId` 5 whereagent1.`Name` not likeCONCAT('%',@Name__4,'%');
五.String.StartsWith() 舉例
1. like 條件
1var res13 = await Conn 2.Queryer(out Agent agent13, out AgentInventoryRecord record13) 3.From(() => agent13) 4.InnerJoin(() => record13) 5.On(() => agent13.Id == record13.AgentId) 6.Where(() => agent13.Name.StartsWith("張")) 7.QueryListAsync<Agent>();
生成 SQL 如下,其中@Name__4 的值會自動生成 '張%'
1 select agent13.`*` 2 from `agent` as agent13 3inner join `agentinventoryrecord` as record13 4on agent13.`Id`=record13.`AgentId` 5 whereagent13.`Name` like@Name__4;
2. not like 條件
1var res22 = await Conn 2.Queryer(out Agent agent22, out AgentInventoryRecord record22) 3.From(() => agent22) 4.InnerJoin(() => record22) 5.On(() => agent22.Id == record22.AgentId) 6.Where(() => !agent22.Name.StartsWith("張")) 7.QueryListAsync<Agent>();
生成 SQL 如下,其中 @Name__4 的值會自動生成'張%'
1 select agent22.`*` 2 from `agent` as agent22 3inner join `agentinventoryrecord` as record22 4on agent22.`Id`=record22.`AgentId` 5 whereagent22.`Name` not like@Name__4;
六.String.EndsWith() 舉例
1. like 條件
1var res13 = await Conn 2.Queryer(out Agent agent13, out AgentInventoryRecord record13) 3.From(() => agent13) 4.InnerJoin(() => record13) 5.On(() => agent13.Id == record13.AgentId) 6.Where(() => agent13.Name.EndsWith("華")) 7.QueryListAsync<Agent>();
生成 SQL 如下,其中 @Name__4 的值會自動生成'%華'
1 select agent13.`*` 2 from `agent` as agent13 3inner join `agentinventoryrecord` as record13 4on agent13.`Id`=record13.`AgentId` 5 whereagent13.`Name` like@Name__4;
2. not like 條件
1var res22 = await Conn 2.Queryer(out Agent agent22, out AgentInventoryRecord record22) 3.From(() => agent22) 4.InnerJoin(() => record22) 5.On(() => agent22.Id == record22.AgentId) 6.Where(() => !agent22.Name.EndsWith("華")) 7.QueryListAsync<Agent>();
生成 SQL 如下,其中 @Name__4 的值會自動生成 '%華'
1 select agent22.`*` 2 from `agent` as agent22 3inner join `agentinventoryrecord` as record22 4on agent22.`Id`=record22.`AgentId` 5 whereagent22.`Name` not like@Name__4;
七.MySQL 萬用字元 %(百分號) 、 _(下劃線) 舉例
1. %
1 var res5 = await Conn.QueryListAsync<Agent>(it => it.Name.Contains("陳%"));
生成 SQL 如下,其中 like 的時候 會保留 原狀 按自定義的 格式串 查詢,@Name__1 的值為 '陳%'
1 select * 2 from `agent` 3 where`Name` like@Name__1;
2. _
1 var res6 = await Conn.QueryListAsync<Agent>(it => it.Name.Contains("王_"));
生成 SQL 如下,其中 like 的時候 會保留 原狀 按自己定義的 格式串 查詢,@Name__1 的值為 '王_'
1 select * 2 from `agent` 3 where`Name` like@Name__1;
八.MySQL 萬用字元轉義 /%(百分號轉義)、/_(下劃線轉義) 舉例
1. /%
1var res7 = await Conn 2.Queryer<Agent>() 3.Where(it => it.Name.Contains("劉/%_")) 4.And(it => it.Id == resx4.Id) 5.And(it => it.Name.Contains("%華")) 6.And(it => it.Name.Contains("%/%%")) 7.QueryListAsync();
生成 SQL 如下,其中 @Name__1 的值為 '劉/% _' ,% 會按其字面義 在DB中匹配查詢
1 select * 2 from `agent` 3 where`Name` like@Name__1 escape '/' 4and`Id`=@Id__2 5and`Name` like@Name__3 6and`Name` like@Name__4 escape '/';
2. /_
1var res8 = await Conn.QueryListAsync<Agent>(it => it.Name.Contains("何/__"));
生成 SQL 如下,其中 @Name__1 的值為 '何/_ _' ,_ 會按其字面義 在DB中匹配查詢
1 select * 2 from `agent` 3 where`Name` like@Name__1 escape '/';
蒙
2019-02-18 14:45 週一