執行資料選取,相對困難的部份在於條件設定,這也是影響 ORM 好不好用的關鍵之一,目前 Kuick 尚未完全實作支援所有 LINQ 功能,在便利性上的確有些落差,但是已經能透過 lambda 表達式進行欄位選取、指定欄位值與邏輯比較,並且針對 SQL Command 解析進行優化,接下來的 CRUD 完結篇就來說明這部份。
<分頁>
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.Paging(10, 2)
.Query();
<排序>
遞增排序
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.Ascending(x => x.Email) //遞增排序
.Query();
遞減排序
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
. Descending(x => x.Email) //遞減排序
.Query();
<一般查詢條件>
使用Where方法,以lambda 指定查詢條件
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.Where(x =>
(
x.Level > 6
|
x.Flag == true
)
&
x.CreateDate > new DateTime(2013, 10, 1)
)
.Query();
Log
2013-10-10 20:17:45.529 153 >>> ZU5P7RUBY4MERMBCQ5TTZNVEUU Track
Title : Api.Query
1. Elapsed (seconds) = 0.034
2. Connection SessionID = IV54QPCVZIHUNNXO4TX4HFNFBE
3. SQL Command String = SELECT [K13_].*
FROM [T_EMPLOYEE] [K13_]
WHERE ((([LEVEL] > 6) OR ([FLAG] = 1)) AND ([CREATE_DATE] > CONVERT(DATETIME, '2013-10-01 00:00:00.000', 21)))
4. Record Count = 2
<Like 條件>
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.Like(x => x.FullName, "鍾")
.Query();
Log
2013-10-10 20:23:02.891 67 >>> D3XL3CIDNVYE5E25HATNPTY76U Track
Title : Api.Query
1. Elapsed (seconds) = 0.147
2. Connection SessionID = 7HE5XS3FNQSU3DOEJT4BTFH5NA
3. SQL Command String = SELECT [K13_].*
FROM [T_EMPLOYEE] [K13_]
WHERE [FULL_NAME] LIKE N'%鍾%'
4. Record Count = 2
<IsNullOrEmpty 條件>
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.IsNullOrEmpty(x => x.Email)
.Query();
Log
2013-10-10 20:28:24.556 67 >>> S26KRSCGCFYUDO554NUKWAQGNE Track
Title : Api.Query
1. Elapsed (seconds) = 0.038
2. Connection SessionID = PPMY6VGN66RUBCS2QVUZOCUMXE
3. SQL Command String = SELECT [K13_].*
FROM [T_EMPLOYEE] [K13_]
WHERE ([EMAIL] = N'' OR [EMAIL] IS NULL)
4. Record Count = 2
<In/NotIn 條件>
List<EmployeeEntity> employees = EmployeeEntity
.Sql()
.In(x => x.Level, 2, 6, 8)
.Query();
Log
2013-10-10 20:36:12.683 67 >>> 6MQF67P3JAMERNNQ3MXGDTPCXQ Track
Title : Api.Query
1. Elapsed (seconds) = 0.010
2. Connection SessionID = V2WMKOMF4SBUXG3RHEEKMZWHSY
3. SQL Command String = SELECT [K13_].*
FROM [T_EMPLOYEE] [K13_]
WHERE [LEVEL] IN (2, 6, 8)
4. Record Count = 1
<Join 查詢>
List<OrderProductEntity> ops = OrderProductEntity
.Sql()
.Join<ProductEntity>(x => x.ProductID, y => y.ProductID)
.Where<ProductEntity>(x => x.Flag == true)
.Where(x => x.Quantity > 100)
.Query();
foreach(var op in ops) {
// op 是 OrderProductEntity
// 透過 GetJoin 方法,可以取得 ProductEntity
ProductEntity p = op.GetJoin<ProductEntity>();
// ...
}
Log
2013-10-10 20:49:24.286 68 >>> NM45JDLNCY2EXGWWZ2A3JUX5FI Track
Title : Api.Query
1. Elapsed (seconds) = 0.057
2. Connection SessionID = AMCAH5ENO6KEROQLJTZKQ4WBCA
3. SQL Command String = SELECT [K9_].[ORDER_PRODUCT_ID] AS K9_ORDER_PRODUCT_ID, [K9_].[ORDER_ID] AS K9_ORDER_ID, [K9_].[PRODUCT_ID] AS K9_PRODUCT_ID, [K9_].[QUANTITY] AS K9_QUANTITY, [K9_].[UNIT_PRICE] AS K9_UNIT_PRICE, [K9_].[CREATE_DATE] AS K9_CREATE_DATE, [K9_].[LAST_MODIFIED_DATE] AS K9_LAST_MODIFIED_DATE, [K9_].[FLAG] AS K9_FLAG, [K9_].[VERSION_NUMBER] AS K9_VERSION_NUMBER, [K14_].[PRODUCT_ID] AS K14_PRODUCT_ID, [K14_].[BRAND_ID] AS K14_BRAND_ID, [K14_].[NAME] AS K14_NAME, [K14_].[UNIT] AS K14_UNIT, [K14_].[COST] AS K14_COST, [K14_].[FIXED_PRICE] AS K14_FIXED_PRICE, [K14_].[CREATE_DATE] AS K14_CREATE_DATE, [K14_].[LAST_MODIFIED_DATE] AS K14_LAST_MODIFIED_DATE, [K14_].[FLAG] AS K14_FLAG, [K14_].[VERSION_NUMBER] AS K14_VERSION_NUMBER
FROM [T_ORDER_PRODUCT] [K9_]
LEFT OUTER JOIN [T_PRODUCT] [K14_] ON [K14_].[PRODUCT_ID] = [K9_].[PRODUCT_ID]
WHERE (([K9_].[FLAG] = 1)) AND (([QUANTITY] > 100))