欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyDAL - .FirstOrDefaultAsync() 使用

程序员文章站 2023-03-31 13:34:16
索引: 商业开发实战总结 一.API 列表 .FirstOrDefaultAsync() .FirstOrDefaultAsync() 如: .FirstOrDefaultAsync() , 用于 单表/多表连接 查询. .FirstOrDefaultAsync() 如: ......

索引:

商业开发实战总结

一.api 列表

  .firstordefaultasync() 

  .firstordefaultasync<m>()

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

  .firstordefaultasync<vm>()

    如: .firstordefaultasync<agentvm>() , 用于 单表 查询.

  .firstordefaultasync<t>(expression<func<m, t>> columnmapfunc)

    如: .firstordefaultasync<guid>(it => it.id) , 用于 单表 单列 查询.

    或者:

        .firstordefaultasync<agentvm>(it => new agentvm
                   {
                      xxxx = it.name,
                      yyyy = it.pathid
                   })    , 用于 单表 多列 查询.

  .firstordefaultasync<t>(expression<func<t>> columnmapfunc)

    如: .queryer<agent, agentinventoryrecord>(out var agent, out var agentrecord)

      ......

      .firstordefaultasync<string>(() => agent.name)

      用于 多表连接 单列 查询.

    或者:

      .queryer<agent, agentinventoryrecord>(out var agent2, out var record2)

      ......

      .firstordefaultasync(() => 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          .firstordefaultasync<alipaypaymentrecord, guid>(it => it.id == pk && it.createdon == date,it=>it.id);

    生成 sql 如下

1 select `id`
2 from `alipaypaymentrecord`
3 where ( `id`=@id__2 &&  `createdon`=@createdon__3)
4 order by `createdon` desc
5 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
           .firstordefaultasync<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 });

    生成 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 order by `createdon` desc
7 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          .firstordefaultasync<alipaypaymentrecord, alipaypaymentrecordvm>(it => it.id == pk && it.createdon == date);

    生成 sql 如下

1 select     `id`,
2     `createdon`,
3     `totalamount`,
4     `description`,
5     `canceledon`
6 from `alipaypaymentrecord`
7 where ( `id`=@id__2 &&  `createdon`=@createdon__3)
8 order by `createdon` desc
9 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          .firstordefaultasync<alipaypaymentrecord>(it => it.id == pk && it.createdon == date);

    生成 sql 如下

1 select *
2 from `alipaypaymentrecord`
3 where ( `id`=@id__2 &&  `createdon`=@createdon__3)
4 order by `createdon` desc
5 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                 .firstordefaultasync<guid>(it => it.id);

    生成 sql 如下

1 select `id`
2 from `agent`
3 where  `createdon`=@createdon__1
4 order by `createdon` desc
5 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                 .firstordefaultasync<agentvm>(it => new agentvm
5                 {
6                     xxxx = it.name,
7                     yyyy = it.pathid
8                 });

    生成 sql 如下

1 select     `name` as xxxx,
2     `pathid` as yyyy
3 from `agent`
4 where  `id`=@id__1
5 order by `createdon` desc
6 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                 .firstordefaultasync<agentvm>();

    生成 sql 如下

 1 select     `id`,
 2     `createdon`,
 3     `userid`,
 4     `pathid`,
 5     `name`,
 6     `phone`
 7 from `agent`
 8 where  `id`=@id__1
 9 order by `createdon` desc
10 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                 .firstordefaultasync();

    生成 sql 如下

1 select *
2 from `bodyfitrecord`
3 where  `id`=@id__1
4 order by `createdon` desc
5 limit 0,1;

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

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

1             var res1 = await conn
2                 .queryer<agent, agentinventoryrecord>(out var agent, out var agentrecord)
3                 .from(() => agent)
4                     .innerjoin(() => agentrecord)
5                         .on(() => agent.id == agentrecord.agentid)
6                 .where(() => agent.agentlevel == agentlevel.distiagent)
7                 .firstordefaultasync<string>(() => agent.name);

    生成 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 order by agent.`createdon` desc
7 limit 0,1;

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

 1             var guid2 = guid.parse("544b9053-322e-4857-89a0-0165443dcbef");
 2 
 3             var res2 = await conn
 4                 .queryer<agent, agentinventoryrecord>(out var agent2, out var record2)
 5                 .from(() => agent2)
 6                     .innerjoin(() => record2)
 7                         .on(() => agent2.id == record2.agentid)
 8                 .where(() => agent2.id == guid2)
 9                 .firstordefaultasync(() => new agentvm
10                 {
11                     nn = agent2.pathid,
12                     yy = record2.id,
13                     xx = agent2.id,
14                     zz = agent2.name,
15                     mm = record2.lockedcount
16                 });

    生成 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 order by agent2.`createdon` desc
11 limit 0,1;

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

1             var guid6 = guid.parse("544b9053-322e-4857-89a0-0165443dcbef");
2 
3             var res6 = await conn
4                 .queryer<agent, agentinventoryrecord>(out var agent6, out var record6)
5                 .from(() => agent6)
6                     .innerjoin(() => record6)
7                         .on(() => agent6.id == record6.agentid)
8                 .where(() => agent6.id == guid6)
9                 .firstordefaultasync<agent>();

    生成 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 order by agent6.`createdon` desc
7 limit 0,1;

 

 

 

 

 

                                         蒙

                                    2018-12-13 14:35 周四