博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MyDAL - like && not like 条件 使用
阅读量:4685 次
发布时间:2019-06-09

本文共 8168 字,大约阅读时间需要 27 分钟。

索引:

一.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
(it => it.Name.Contains("陈"));

    以 MySQL 为例,生成 SQL 如下:

1 select *2 from `agent`3 where  `Name` like  CONCAT('%',?Name_1,'%');

   2. not like 条件

1 var res1 = await Conn.QueryListAsync
(it => !it.Name.Contains("刘"));

    以 MySQL 为例,生成 SQL 如下:

1 select *2 from `agent`3 where  `Name` not like  CONCAT('%',?Name_1,'%');

三.API 单表-完整 方法 举例

  1. like 条件

1             var res1 = await Conn2                 .Queryer
()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 .QueryPagingAsync(1, 10);

    以 MySQL 为例,生成 SQL 如下:

1 -- 总数 2 select  count(*)  3 from `agent` 4 where  `CreatedOn`>=?CreatedOn_1 5     and  `PathId` like  CONCAT('%',?PathId_2,'%'); 6  7 -- 分页数据 8 select * 9 from `agent`10 where  `CreatedOn`>=?CreatedOn_111     and  `PathId` like  CONCAT('%',?PathId_2,'%')12 order by `Id` desc13 limit 0,10;

  2. not like 条件

1             var res1 = await Conn2                 .Queryer
()3 .Where(it => !it.PathId.Contains("~00-d-3-1-"))4 .QueryPagingAsync(1, 10);

    以 MySQL 为例,生成 SQL 如下:

1 -- 总数 2 select  count(*)  3 from `agent` 4 where  `PathId` not like  CONCAT('%',?PathId_1,'%'); 5  6 -- 分页数据 7 select * 8 from `agent` 9 where  `PathId` not like  CONCAT('%',?PathId_1,'%')10 order by `Id` desc11 limit 0,10;

四.API 多表连接-完整 方法 举例

  1. like 条件

1             var res1 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下:

1 select record1.`*`2 from `agent` as agent1 3     inner join `agentinventoryrecord` as record14         on agent1.`Id`=record1.`AgentId`5 where  agent1.`Name` like  CONCAT('%',?Name_4,'%');

  2. not like 条件

1             var res1 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下:

1 select record1.`*`2 from `agent` as agent1 3     inner join `agentinventoryrecord` as record14         on agent1.`Id`=record1.`AgentId`5 where  agent1.`Name` not like  CONCAT('%',?Name_4,'%');

五.String.StartsWith() 举例

  1. like 条件

1             var res13 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_4 的值会自动生成 '张%'

1 select agent13.`*`2 from `agent` as agent13 3     inner join `agentinventoryrecord` as record134         on agent13.`Id`=record13.`AgentId`5 where  agent13.`Name` like  ?Name_4;

  2. not like 条件

1             var res22 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_4 的值会自动生成 '张%'

1 select agent22.`*`2 from `agent` as agent22 3     inner join `agentinventoryrecord` as record224         on agent22.`Id`=record22.`AgentId`5 where  agent22.`Name` not like  ?Name_4;

六.String.EndsWith() 举例

  1. like 条件

1             var res13 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_4 的值会自动生成 '%华'

1 select agent13.`*`2 from `agent` as agent13 3     inner join `agentinventoryrecord` as record134         on agent13.`Id`=record13.`AgentId`5 where  agent13.`Name` like  ?Name_4;

  2. not like 条件

1             var res22 = await Conn2                 .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
();

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_4 的值会自动生成 '%华'

1 select agent22.`*`2 from `agent` as agent22 3     inner join `agentinventoryrecord` as record224         on agent22.`Id`=record22.`AgentId`5 where  agent22.`Name` not like  ?Name_4;

七.MySQL 通配符 %(百分号) 、 _(下划线) 举例

   1. %

1 var res5 = await Conn.QueryListAsync
(it => it.Name.Contains("陈%"));

    以 MySQL 为例,生成 SQL 如下,其中 like 的时候 会保留 原状 按自定义的 格式串 查询,?Name_1 的值为 '陈%'

1 select *2 from `agent`3 where  `Name` like  ?Name_1;

  2. _

1 var res6 = await Conn.QueryListAsync
(it => it.Name.Contains("王_"));

    以 MySQL 为例,生成 SQL 如下,其中 like 的时候 会保留 原状 按自己定义的 格式串 查询,?Name_1 的值为 '王_'

1 select *2 from `agent`3 where  `Name` like  ?Name_1;

八.MySQL 通配符转义 /%(百分号转义)、/_(下划线转义) 举例

  1. /%

1             var res7 = await Conn2                 .Queryer
()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();

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_1 的值为 '刘/%_' ,% 会按其 字面义 在DB中匹配查询

1 select *2 from `agent`3 where  `Name` like  ?Name_1 escape '/'4     and  `Id`=?Id_25     and  `Name` like  ?Name_36     and  `Name` like  ?Name_4 escape '/';

  2. /_

1             var res8 = await Conn.QueryListAsync
(it => it.Name.Contains("何/__"));

    以 MySQL 为例,生成 SQL 如下,其中 ?Name_1 的值为 '何/__' ,_ 会按其 字面义 在DB中匹配查询

1 select *2 from `agent`3 where  `Name` like  ?Name_1 escape '/';

 

 

 

 

                                         蒙

                                    2019-02-18 14:45 周一

                                    2019-02-24 17:50 周日

                                    2019-04-13 00:29 周六

 

转载于:https://www.cnblogs.com/Meng-NET/p/10387628.html

你可能感兴趣的文章
git常用操作
查看>>
京东SSO单点登陆实现分析
查看>>
render()方法是render_to_response
查看>>
u-boot启动第一阶段
查看>>
北京大学2019年数学分析考研试题
查看>>
MySQL批量SQL插入性能优化
查看>>
定义列属性:null,default,PK,auto_increment
查看>>
用户画像展示
查看>>
pyqt pyinstaller使用说明
查看>>
C#中StreamReader读取中文出现乱码
查看>>
引用堆中的对象
查看>>
用CSS开启硬件加速来提高网站性能(转)
查看>>
使用BufferedReader的时候出现的问题
查看>>
加快页面加载速度的方法
查看>>
Oozie协作框架
查看>>
linux安装图形界面
查看>>
Android广播发送失败
查看>>
博弈论之入门小结
查看>>
解决IE8下opacity属性失效问题,无法隐藏元素
查看>>
洛谷1002 过河卒
查看>>