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

索引:html

目錄索引es6

一.API 列表sql

  1.Whereapi

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

      如: .Where( it => (it.Prop1>=條件1 && it.Prop2<=條件2) || it.Prop3==條件3 ) 此類寫法,spa

        用在 Deleter/Updater/Queryer(單表) 中.3d

    .Where(Func<bool>)code

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

  2.Andblog

    .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)..... .

二.使用舉例

  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();

     以 MySQL 爲例,生成 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();

    以 MySQL 爲例,生成 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                 .Queryer<Agent>() 6                 .Where(it => it.Id == guid4 && it.AgentLevel==level4  && it.PathId == pathId4) 7                 .QueryListAsync();

    以 MySQL 爲例,生成 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                 .Queryer(out Agent agent6, out AgentInventoryRecord record6) 3                 .From(() => agent6)
4                     .InnerJoin(() => record6)
5                         .On(() => agent6.Id == record6.AgentId)
6                 .Where(() => agent6.Id == guid6) 7                 .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 order by agent6.`CreatedOn` desc ;

 

 

 

 

                                         蒙

                                    2018-11-18 16:32 週日

                                    2018-12-13 15:27 週四

                                    2018-12-30 11:15 週日

                                    2019-02-24 17:45 週日

                                    2019-04-12 18:04 週五

相關文章
相關標籤/搜索