首页
登录 | 注册

MyDAL - .QueryOneAsync() 使用

索引:

目录索引

一.API 列表

  .QueryOneAsync() 

  .QueryOneAsync<M>()

    如: .QueryOneAsync<Agent>() , 用于 单表/多表连接 查询.

  .QueryOneAsync<VM>()

    如: .QueryOneAsync<AgentVM>() , 用于 单表 查询.

  .QueryOneAsync<T>(Expression<Func<M, T>> columnMapFunc)

    如: .QueryOneAsync<Guid>(it => it.Id) , 用于 单表 单列 查询.

    或者:

        .QueryOneAsync<AgentVM>(it => new AgentVM
                   {
                      XXXX = it.Name,
                      YYYY = it.PathId
                   })    , 用于 单表 多列 查询.

  .QueryOneAsync<T>(Expression<Func<T>> columnMapFunc)

    如: .Queryer(out Agent agent, out AgentInventoryRecord agentRecord)

      ......

      .QueryOneAsync<string>(() => agent.Name)

      用于 多表连接 单列 查询.

    或者:

      .Queryer(out Agent agent2, out AgentInventoryRecord record2)

      ......

      .QueryOneAsync(() => new AgentVM
                   {
                      nn = agent2.PathId,
                      yy = record2.Id,
                      xx = agent2.Id,
                      zz = agent2.Name,
                      mm = record2.LockedCount
                     })  , 用于 多表连接 多列 查询.

二.API 单表-便捷 方法 举例

  1. 单表 单列 便捷方法    

1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
2             var date= DateTime.Parse("2018-08-20 19:12:05.933786");
3 
4             var res3 = await Conn
5          .QueryOneAsync<AlipayPaymentRecord, Guid>(it => it.Id == pk && it.CreatedOn == date,it=>it.Id);

    以 MySQL 为例,生成 SQL 如下:

1 select `Id`
2 from `AlipayPaymentRecord`
3 where ( `Id`=?Id_2 &&  `CreatedOn`=?CreatedOn_3)
4 limit 0,1;

  2. 单表 多列 便捷方法

 1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
 2             var date= DateTime.Parse("2018-08-20 19:12:05.933786");
 3 
 4             var res4 = await Conn
           .QueryOneAsync<AlipayPaymentRecord, AlipayPaymentRecordVM>(it => it.Id == pk && it.CreatedOn == date, 5 it => new AlipayPaymentRecordVM 6 { 7 Id = it.Id, 8 TotalAmount = it.TotalAmount, 9 Description = it.Description 10 });

    以 MySQL 为例,生成 SQL 如下:

1 select     `Id` as Id,
2     `TotalAmount` as TotalAmount,
3     `Description` as Description
4 from `AlipayPaymentRecord`
5 where ( `Id`=?Id_2 &&  `CreatedOn`=?CreatedOn_3)
6 limit 0,1;

  3. 单表 单条 VM 便捷方法

1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
2             var date = DateTime.Parse("2018-08-20 19:12:05.933786");
3 
4             var res5 = await Conn
5          .QueryOneAsync<AlipayPaymentRecord, AlipayPaymentRecordVM>(it => it.Id == pk && it.CreatedOn == date);

    以 MySQL 为例,生成 SQL 如下:

1 select     `Id`,
2     `CreatedOn`,
3     `TotalAmount`,
4     `Description`,
5     `CanceledOn`
6 from `AlipayPaymentRecord`
7 where ( `Id`=?Id_2 &&  `CreatedOn`=?CreatedOn_3)
8 limit 0,1;

  4. 单表 单条 M 便捷方法

1             var pk = Guid.Parse("8f2cbb64-8356-4482-88ee-016558c05b2d");
2             var date = DateTime.Parse("2018-08-20 19:12:05.933786");
3 
4             var res6 = await Conn
5          .QueryOneAsync<AlipayPaymentRecord>(it => it.Id == pk && it.CreatedOn == date);

    以 MySQL 为例,生成 SQL 如下:

1 select *
2 from `AlipayPaymentRecord`
3 where ( `Id`=?Id_2 &&  `CreatedOn`=?CreatedOn_3)
4 limit 0,1;

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

  1. 单表 单列 完整方法

1             var time1 = DateTime.Parse("2018-08-16 19:22:01.716307");
2 
3             var res1 = await Conn
4                 .Queryer<Agent>()
5                 .Where(it => it.CreatedOn == time1)
6                 .QueryOneAsync<Guid>(it => it.Id);

    以 MySQL 为例,生成 SQL 如下:

1 select `Id`
2 from `Agent`
3 where  `CreatedOn`=?CreatedOn_1
4 limit 0,1;

   2.单表 多列 完整方法

1             var res3 = await Conn
2                 .Queryer<Agent>()
3                 .Where(it => it.Id == Guid.Parse("000c1569-a6f7-4140-89a7-0165443b5a4b"))
4                 .QueryOneAsync<AgentVM>(it => new AgentVM
5                 {
6                     XXXX = it.Name,
7                     YYYY = it.PathId
8                 });

    以 MySQL 为例,生成 SQL 如下:

1 select     `Name` as XXXX,
2     `PathId` as YYYY
3 from `Agent`
4 where  `Id`=?Id_1
5 limit 0,1;

  3.单表 单条 VM 完整方法

1             var res1 = await Conn
2                 .Queryer<Agent>()
3                 .Where(it => it.Id == Guid.Parse("000c1569-a6f7-4140-89a7-0165443b5a4b"))
4                 .QueryOneAsync<AgentVM>();

    以 MySQL 为例,生成 SQL 如下:

 1 select     `Id`,
 2     `CreatedOn`,
 3     `UserId`,
 4     `PathId`,
 5     `Name`,
 6     `Phone`
 7 from `Agent`
 8 where  `Id`=?Id_1
 9 limit 0,1;

  4.单表 单条 M 完整方法

1             var res1 = await Conn
2                 .Queryer<BodyFitRecord>()
3                 .Where(it => it.Id == Guid.Parse("1fbd8a41-c75b-45c0-9186-016544284e2e"))
4                 .QueryOneAsync();

    以 MySQL 为例,生成 SQL 如下:

1 select *
2 from `BodyFitRecord`
3 where  `Id`=?Id_1
4 limit 0,1;

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

  1.多表连接 单列 完整方法

1             var res1 = await Conn
2                 .Queryer(out Agent agent, out AgentInventoryRecord agentRecord)
3                 .From(() => agent)
4                     .InnerJoin(() => agentRecord)
5                         .On(() => agent.Id == agentRecord.AgentId)
6                 .Where(() => agent.AgentLevel == AgentLevel.DistiAgent)
7                 .QueryOneAsync<string>(() => agent.Name);

    以 MySQL 为例,生成 SQL 如下:

1 select agent.`Name`
2 from `Agent` as agent 
3     inner join AgentInventoryRecord as agentRecord
4         on agent.`Id`=agentRecord.`AgentId`
5 where  agent.`AgentLevel`=?AgentLevel_4
6 limit 0,1;

  2.多表连接 多列 完整方法

 1             var guid2 = Guid.Parse("544b9053-322e-4857-89a0-0165443dcbef");
 2 
 3             var res2 = await Conn
 4                 .Queryer(out Agent agent2, out AgentInventoryRecord record2)
 5                 .From(() => agent2)
 6                     .InnerJoin(() => record2)
 7                         .On(() => agent2.Id == record2.AgentId)
 8                 .Where(() => agent2.Id == guid2)
 9                 .QueryOneAsync(() => new AgentVM
10                 {
11                     nn = agent2.PathId,
12                     yy = record2.Id,
13                     xx = agent2.Id,
14                     zz = agent2.Name,
15                     mm = record2.LockedCount
16                 });

    以 MySQL 为例,生成 SQL 如下:

 1 select     agent2.`PathId` as nn,
 2     record2.`Id` as yy,
 3     agent2.`Id` as xx,
 4     agent2.`Name` as zz,
 5     record2.`LockedCount` as mm
 6 from `Agent` as agent2 
 7     inner join AgentInventoryRecord as record2
 8         on agent2.`Id`=record2.`AgentId`
 9 where  agent2.`Id`=?Id_4
10 limit 0,1;

  3.多表连接 单条 M 完整方法

1             var guid6 = Guid.Parse("544b9053-322e-4857-89a0-0165443dcbef");
2 
3             var res6 = await Conn
4                 .Queryer(out Agent agent6, out AgentInventoryRecord record6)
5                 .From(() => agent6)
6                     .InnerJoin(() => record6)
7                         .On(() => agent6.Id == record6.AgentId)
8                 .Where(() => agent6.Id == guid6)
9                 .QueryOneAsync<Agent>();

    以 MySQL 为例,生成 SQL 如下:

1 select agent6.`*`
2 from `Agent` as agent6 
3     inner join AgentInventoryRecord as record6
4         on agent6.`Id`=record6.`AgentId`
5 where  agent6.`Id`=?Id_4
6 limit 0,1;

 

 

 

 

 

                                         蒙

                                    2018-12-13 14:35 周四

                                    2018-12-30 11:25 周日

                                    2019-02-24 17:03 周日

                                    2019-04-12 19:32 周五

 



2019 cecdns.com webmaster#cecdns.com
11 q. 0.074 s.
京ICP备10005923号