前情提要
上篇[DotnetCore]ORM系列-Chloe:入門篇介紹Chloe
的使用方式,這篇主要繼續探討,Chloe
提供哪些Select相關的Method,跟著筆者一起看下去。跟著Chloe github的wiki針對筆者之前建立的範例資料庫做一個應用吧。
內容
筆者這邊就照著github wiki上的教學,針對筆者自己建的範例資料庫,做一個實際應用的Demo,每一段都會有他自己的Conditions Model
,對應的Service
及Controller
的宣告。
基礎查詢
這次所有範例的Service相關的Interface宣告如下
1 2 3 4 5 6 7 8 9
| public interface ICustomerService { Customers GetCustomerById(int customerNumber); IEnumerable<Customers> GetCustomerListByLikeConditions(CustomerLikeConditions conditions); IEnumerable<Customers> GetCustomersListByInConditions(CustomerInConditions conditions); IEnumerable<Customers> GetCustomersListByPaginationConditions(CustomerPageConditions conditions); List<string> GetCustomersCountryListByDistinct(); IEnumerable<Customers> GetCustomersListByExists(CustomerExistConditions conditions); }
|
對應的API Url
如下
![[CustomerAPI-Swagger]](/2021/09/07/orm-series-chloe-select/Untitled.png)
[CustomerAPI-Swagger]
PrimaryKey過濾
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public Customers GetCustomerById(int customerNumber) { return _db.Query<Customers>() .FirstOrDefault(x => x.CustomerNumber == customerNumber); }
[ProducesResponseType(typeof(Customers), StatusCodes.Status200OK)] [HttpPost("{customerNumber}")] public IActionResult GetCustomerById(int customerNumber) { return Ok(_customerService.GetCustomerById(customerNumber)); }
|
![[CustomerService-GetCustomerById]](/2021/09/07/orm-series-chloe-select/Untitled1.png)
[CustomerService-GetCustomerById]
Like查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class CustomerLikeConditions { public string CustomerName { get; set; } }
public IEnumerable<Customers> GetCustomerListByLikeConditions(CustomerLikeConditions conditions) { return _db.Query<Customers>() .Where(x => x.CustomerName.StartsWith(conditions.CustomerName)) .ToList(); }
[ProducesResponseType(typeof(IEnumerable<Customers>), StatusCodes.Status200OK)] [HttpPost("like")] public IActionResult GetCustomerListByLikeConditions(CustomerLikeConditions conditions) { return Ok(_customerService.GetCustomerListByLikeConditions(conditions)); }
|
![[CustomerService-GetCustomerListByLike]](/2021/09/07/orm-series-chloe-select/Untitled2.png)
[CustomerService-GetCustomerListByLike]
In查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class CustomerInConditions { public List<string> CustomerNameList { get; set; } }
public IEnumerable<Customers> GetCustomersListByInConditions(CustomerInConditions conditions) { return _db.Query<Customers>() .Where(x => conditions.CustomerNameList.Contains(x.CustomerName)) .ToList(); }
[ProducesResponseType(typeof(IEnumerable<Customers>), StatusCodes.Status200OK)] [HttpPost("in")] public IActionResult GetCustomerListByInConditions(CustomerInConditions conditions) { return Ok(_customerService.GetCustomersListByInConditions(conditions)); }
|
![[CustomerService-GetCustomerListByIn]](/2021/09/07/orm-series-chloe-select/Untitled3.png)
[CustomerService-GetCustomerListByIn]
排序分頁查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class BasePaginationParameters { public virtual int PageNumber { get; set; } = 1; public virtual int PageSize { get; set; } = 10; } public class CustomerPageConditions : BasePaginationParameters {
}
public IEnumerable<Customers> GetCustomersListByPaginationConditions(CustomerPageConditions conditions) { return _db.Query<Customers>() .OrderBy(x => x.CustomerName) .ThenBy(x => x.CustomerNumber) .TakePage(conditions.PageNumber, conditions.PageSize) .ToList(); }
[ProducesResponseType(typeof(IEnumerable<Customers>), StatusCodes.Status200OK)] [HttpPost("pagination")] public IActionResult GetCustomerListByPagination(CustomerPageConditions conditions) { return Ok(_customerService.GetCustomersListByPaginationConditions(conditions)); }
|
![[CustomerService-GetCustomerListByPagSize:10]](/2021/09/07/orm-series-chloe-select/Untitled4.png)
[CustomerService-GetCustomerListByPagSize:10]
![[CustomerService-GetCustomerListByPageSize:20]](/2021/09/07/orm-series-chloe-select/Untitled5.png)
[CustomerService-GetCustomerListByPageSize:20]
Distinct查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| public List<string> GetCustomersCountryListByDistinct() { return _db.Query<Customers>() .Select(x => x.Country) .Distinct() .ToList(); }
[ProducesResponseType(typeof(List<string>), StatusCodes.Status200OK)] [HttpPost("distinct")] public IActionResult GetCustomerCountryDistinctList() { return Ok(_customerService.GetCustomersCountryListByDistinct()); }
|
![[CustomerService-GetCustomerListByDistinct]](/2021/09/07/orm-series-chloe-select/Untitled6.png)
[CustomerService-GetCustomerListByDistinct]
Exists查詢
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class CustomerExistConditions { public string CountryName { get; set; } }
public IEnumerable<Customers> GetCustomersListByExists(CustomerExistConditions conditions) { return _db.Query<Customers>() .Where(x => _db.Query<Customers>() .Where(c => c.Country == conditions.CountryName && c.CustomerNumber == x.CustomerNumber) .Any()) .ToList(); }
[ProducesResponseType(typeof(IEnumerable<Customers>), StatusCodes.Status200OK)] [HttpPost("exists")] public IActionResult GetCustomerListByExists(CustomerExistConditions conditions) { return Ok(_customerService.GetCustomersListByExists(conditions)); }
|
![[CustomerService-GetCustomerListByExists]](/2021/09/07/orm-series-chloe-select/Untitled7.png)
[CustomerService-GetCustomerListByExists]
結論
經由這篇介紹基本查詢部份,筆者覺得還滿方便使用的,可以應付日常使用,使用linq形式來過濾資料就是舒服,強型別Intellisense
才是王道啊,下一篇撰寫Chloe
提供的Interceptor
,來探究一下,經由這樣的宣告,Chloe
幫我們產生了什麼對應的sql語法,經由這樣驗證,才會更了解應用,就下篇見了。