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/Queryer(單表) 中.

    .Where(Func<bool>)

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

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

二.使用舉例

  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 周五

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM