首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

Hxj.Data WhereClip 條件建構:運算子多載、And/Or、優先順序括號、WhereClip.All 空條件

     編輯:關於ASP.NET

WhereClip:強型別條件物件,而非字串

基本比較運算子多載

C#SQL
>>
>=>=
<=<=
<<
===
!=<>

邏輯組合:&& (And) / || (Or)

// UnitPrice > 1 且 CategoryID = 2
Products._.UnitPrice > 1 && Products._.CategoryID == 2

// UnitPrice > 1 或 CategoryID = 2
Products._.UnitPrice > 1 || Products._.CategoryID == 2

等效方法呼叫風格

WhereClip where = WhereClip.All; // 空條件,不產生 WHERE
where = where.And(Products._.UnitPrice > 1);
where = where.Or(Products._.CategoryID == 2);

WhereClip.All 代表無條件,And/Or 回傳新 WhereClip(不可變模式)。

優先順序控制:括號 ()

(Products._.ProductName.Contain("apple") && Products._.UnitPrice > 1) || Products._.CategoryID == 2

對應 SQL:(ProductName LIKE '%apple%' AND UnitPrice > 1) OR CategoryID = 2

方法鏈等效寫法

WhereClip where = WhereClip.All;
where = where.And(Products._.ProductName.Contain("apple"));
where = where.And(Products._.UnitPrice > 1);
where = where.Or(Products._.CategoryID == 2);

括號對應巢狀 And/Or 呼叫順序,建議使用運算子寫法更直觀。

WhereClip 核心特性

  • 不可變:每次組合產生新實例,執行緒安全。
  • 強型別:Field 與值型別不符編譯報錯。
  • 參數化:值自動轉為參數,防注入。
  • 可序列化:支援跨層傳遞(如 WCF 服務傳遞查詢條件)。

WhereClip 將 SQL WHERE 子句昇華為一級物件,讓條件建構享有完整的型別安全、智慧提示、重構支援與測試能力。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved