1. 程式人生 > >MyDAL - .Where() & .And() & .Or() 使用

MyDAL - .Where() & .And() & .Or() 使用

索引:

商業開發實戰總結

一.API 列表

  1.Where

    .Where(Func<M, bool> func)  如: .Where( it => (it.Prop1>=條件1 && it.Prop2<=條件2) || it.Prop3==條件3 ) 此類寫法,用在 Deleter/Updater/Selecter 中.

    .Where(Func<bool>) 如: .Where( () => m1.PropX==條件1 || m2.PropY>條件2 && m3.PropZ<條件3 ) 此類寫法,用在 Joiner 中.

  2.And

    .And(Func<M, bool> func)  .Where(Func<M, bool> func) .

    .And(Func<bool>)  .Where(Func<bool>) .

  3.Or

    .Or(Func<M, bool> func)  .Where(Func<M, bool> func) .

    .Or(Func<bool>)  .Where(Func<bool>) .

  注:

where and or 三個 api 是可以組合使用的,我在這裡將他們的關係 處理為 sql 中同樣的用法,即一個 sql 查詢中只可以點出一個where,但可以點出多個

    and 或 or 如:  ...Where(xxx).And(yyy).Or(mmm).And(zzz)..... .

二.使用 Demo 

  1.刪除資料

1             var path = "~00-c-1-2-1-1-1-1-1-4-1-1-1-4-1-2-1-7";
2             var level = 2;
3             // where and
4 var res3 = await Conn 5 .Deleter<Agent>() 6 .Where(it => it.PathId == path) 7 .And(it => it.AgentLevel == (AgentLevel)level) 8 .DeleteAsync();

     生成的 SQL 如下

1 delete
2 from `Agent`   
3  where  `PathId`=@PathId__1   
4       and  `AgentLevel`=@AgentLevel__2 ;

  2.更新資料

1             var res8 = await Conn
2                 .Updater<Agent>()
3                 .Set(it => it.AgentLevel, AgentLevel.NewCustomer)
4                 .Where(it => it.Id == Guid.Parse("0014f62d-2a96-4b5b-b4bd-01654438e3d4"))
5                 .UpdateAsync();

    生成 SQL 如下

1 update `Agent`
2 set `AgentLevel`=@AgentLevel__1    
3  where  `Id`=@Id__2 ;

  3.單表查詢資料

1             var guid4 = Guid.Parse("000cecd5-56dc-4085-804b-0165443bdf5d");
2             var pathId4 = "~00-d-3-2-1-c-2-f-4-3-1-2-4";
3             var level4 = AgentLevel.Customer;
4             var res4 = await Conn
5                 .Selecter<Agent>()
6                 .Where(it => it.Id == guid4 && it.AgentLevel==level4  && it.PathId == pathId4)
7                 .ListAsync();

    生成 SQL 如下

1 select *
2 from `Agent`   
3  where  (( `Id`=@Id__3  &&  `AgentLevel`=@AgentLevel__4 ) &&  `PathId`=@PathId__5 ) 
4 order by `CreatedOn` desc ;

  4.連線查詢資料

1             var res6 = await Conn
2                 .Joiner<Agent, AgentInventoryRecord>(out var agent6, out var record6)
3                 .From(() => agent6)
4                     .InnerJoin(() => record6)
5                         .On(() => agent6.Id == record6.AgentId)
6                 .Where(() => agent6.Id == guid6)
7                 .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 ;

 

 

 

 

                                         蒙

                                    2018-11-18 16:32 週日