设为首页 加入收藏

TOP

MyDAL - .Where() 之 .WhereSegment 根据条件 动态设置 Select查询条件 使用
2019-09-17 18:56:10 】 浏览:31
Tags:MyDAL .Where .WhereSegment 根据 条件 动态 设置 Select 查询 使用

索引:

目录索引

一.API 列表

  1.WhereSegment 属性,指示 根据条件 动态拼接 where 查询过滤条件

    见如下示例.

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

 1             // 上下文条件 变量
 2             var userId = "08d6036b-0a7e-b07d-b9bd-af03841b3baa";
 3             var firstName = "";
 4 
 5             var where = Conn.Queryer<Agent>().WhereSegment;
 6 
 7             // 根据条件 判断 是否 拼接 UserId 字段 的 过滤条件
 8             if (!userId.IsNullStr())
 9             {
10                 where = where.And(it => it.UserId == Guid.Parse(userId));
11             }
12 
13             // 根据条件 判断 是否 拼接 Name 字段 的 过滤条件
14             if (!firstName.IsNullStr())
15             {
16                 where = where.And(it => it.Name.StartsWith(firstName));
17             }
18 
19             // 对 WhereSegment 设定的条件 进行 select 动作
20             var res1 = await where.QueryListAsync();
21 
22             Assert.True(res1.Count==1);

  以 MySQL 为例,生成 SQL 如下,其中 ?Name_2 的值自动生成为 【伏%】 :

1 select *
2 from `agent` where true 
3     and  `UserId`=?UserId_1
4     and  `Name` like  ?Name_2;

三.API 多表-连接 方法 举例

 1             // 上下文 分页 变量
 2             var pageIndex = 2;
 3             var pageSize = 10;
 4 
 5             // 上下文 条件 变量
 6             var level = (Nullable<AgentLevel>)AgentLevel.DistiAgent;
 7             var pk1 = Guid.Parse("fbad4af4-c160-4e66-a8fc-0165443b4db0");
 8 
 9             // 可 自由混合书写 多个 inner join 或 left join 
10             var where = Conn
11                 .Queryer(out Agent agent, out AgentInventoryRecord record)
12                 .From(() => agent)
13                     .LeftJoin(() => record)
14                         .On(() => agent.Id == record.AgentId).WhereSegment;
15 
16             // 根据条件 判断 是否 拼接 AgentLevel 字段 的 过滤条件
17             if (level != null)
18             {
19                 where = where.And(() => agent.AgentLevel == level);   // and demo
20             }
21 
22             // 根据条件 判断 是否 拼接 Id 字段 的 过滤条件
23             if (pk1 != Guid.Empty)
24             {
25                 where = where.Or(() => agent.Id == pk1);   //  or demo
26             }
27 
28             // 对 WhereSegment 设定的条件 进行 select 动作
29             var res1 = await where.QueryPagingAsync<Agent>(pageIndex, pageSize);
30 
31             Assert.True(res1.Data.Count == 10);
32             Assert.True(res1.TotalCount == 575);

  以 MySQL 为例,生成 SQL 如下:

 1 -- 总数据量 sql
 2 
 3 select count(*) 
 4 from (
 5 select  agent.`*`
 6 from `agent` as agent 
 7     left join `agentinventoryrecord` as record
 8         on agent.`Id`=record.`AgentId` where true 
 9     and  agent.`AgentLevel`=?AgentLevel_4
10     or  agent.`Id`=?Id_5
11          ) temp;
12 
13 -- 分页数据 sql
14 
15 select agent.`*`
16 from `agent` as agent 
17     left join `agentinventoryrecord` as record
18         on agent.`Id`=record.`AgentId` where true 
19     and  agent.`AgentLevel`=?AgentLevel_4
20     or  agent.`Id`=?Id_5
21 order by agent.`Id` desc
22 limit 10,10;

 

 

 

 

                                         蒙

                                    2019-04-18 23:30 周四

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C#获取当前服务器运行程序的根目.. 下一篇新品成熟EMR源码电子病历系统软件..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目