索引:
目录索引
一. API 列表
1.WhereSegment 属性, 指示 根据条件 动态拼接 where 查询过滤条件
见如下示例.
二. API 单表 - 完整 方法 举例
- // 上下文条件 变量
- var userId = "08d6036b-0a7e-b07d-b9bd-af03841b3baa";
- var firstName = "伏";
- var where = Conn.Queryer<Agent>().WhereSegment;
- // 根据条件 判断 是否 拼接 UserId 字段 的 过滤条件
- if (!userId.IsNullStr())
- {
- where = where.And(it => it.UserId == Guid.Parse(userId));
- }
- // 根据条件 判断 是否 拼接 Name 字段 的 过滤条件
- if (!firstName.IsNullStr())
- {
- where = where.And(it => it.Name.StartsWith(firstName));
- }
- // 对 WhereSegment 设定的条件 进行 select 动作
- var res1 = await where.QueryListAsync();
- Assert.True(res1.Count==1);
以 MySQL 为例, 生成 SQL 如下, 其中 ?Name_2 的值自动生成为 [伏 %] :
- select *
- from `agent` where true
- and `UserId`=?UserId_1
- and `Name` like ?Name_2;
三. API 多表 - 连接 方法 举例
- // 上下文 分页 变量
- var pageIndex = 2;
- var pageSize = 10;
- // 上下文 条件 变量
- var level = (Nullable<AgentLevel>)AgentLevel.DistiAgent;
- var pk1 = Guid.Parse("fbad4af4-c160-4e66-a8fc-0165443b4db0");
- // 可 自由混合书写 多个 inner join 或 left join
- var where = Conn
- .Queryer(out Agent agent, out AgentInventoryRecord record)
- .From(() => agent)
- .LeftJoin(() => record)
- .On(() => agent.Id == record.AgentId).WhereSegment;
- // 根据条件 判断 是否 拼接 AgentLevel 字段 的 过滤条件
- if (level != null)
- {
- where = where.And(() => agent.AgentLevel == level); // and demo
- }
- // 根据条件 判断 是否 拼接 Id 字段 的 过滤条件
- if (pk1 != Guid.Empty)
- {
- where = where.Or(() => agent.Id == pk1); // or demo
- }
- // 对 WhereSegment 设定的条件 进行 select 动作
- var res1 = await where.QueryPagingAsync<Agent>(pageIndex, pageSize);
- Assert.True(res1.Data.Count == 10);
- Assert.True(res1.TotalCount == 575);
以 MySQL 为例, 生成 SQL 如下:
-- 总数据量 sql
- select count(*)
- from (
- select agent.`*`
- from `agent` as agent
- left join `agentinventoryrecord` as record
- on agent.`Id`=record.`AgentId` where true
- and agent.`AgentLevel`=?AgentLevel_4
- or agent.`Id`=?Id_5
- ) temp;
-- 分页数据 sql
- select agent.`*`
- from `agent` as agent
- left join `agentinventoryrecord` as record
- on agent.`Id`=record.`AgentId` where true
- and agent.`AgentLevel`=?AgentLevel_4
- or agent.`Id`=?Id_5
- order by agent.`Id` desc
- limit 10,10;
蒙
2019-04-18 23:30 周四
来源: https://www.cnblogs.com/Meng-NET/p/10733275.html