Take(n):取前 n 筆Skip(n):略過前 n 筆TakeWhile / SkipWhile:依條件取/略// 方式 1:Skip + Take(第 6 頁,每頁 10 筆)
var q1 = (from c in db.Customers orderby c.ContactName select c).Skip(50).Take(10);
// 方式 2:Where + Take(需唯一鍵排序,效能較佳)
var q2 = (from p in db.Products where p.ProductID > 50 orderby p.ProductID select p).Take(10);
// 客戶 ID 以 C 開頭
var q = from c in db.Customers where SqlMethods.Like(c.CustomerID, "C%") select c;
// 排除 A_O_T 格式
var q = from c in db.Customers where !SqlMethods.Like(c.CustomerID, "A_O_T") select c;
萬用字元:%(任意長度)、_(單一字元)、[](範圍)、[^](排除範圍)。
// 10 天內出貨的訂單
var q = from o in db.Orders
where SqlMethods.DateDiffDay(o.OrderDate, o.ShippedDate) < 10
select o;
另有 DateDiffHour、DateDiffMinute、DateDiffSecond、DateDiffMonth、DateDiffYear、DateDiffMillisecond。
將查詢邏輯預先編譯為委派,重複執行時省略解析轉譯開銷。
// 1. 建立編譯查詢
var fn = CompiledQuery.Compile(
(NorthwindDataContext db2, string city) =>
from c in db2.Customers where c.City == city select c);
// 2. 重複使用
var LonCusts = fn(db, "London");
var SeaCusts = fn(db, "Seattle");