using System;
using System.Query;
using System.Collections.Generic;
class App {
static void Main() {
string[] names = { "Burke", "Connor", "Frank", "Everett", "Albert", "George", "Harris", "David" };
IEnumerable expr = from s in names
where s.Length == 5
orderby s
select s.ToUpper();
foreach (string item in expr)
Console.WriteLine(item);
}
}
from s in names:s 為陣列元素,隱含宣告於 in 子句中where s.Length == 5:篩選條件orderby s:排序select s.ToUpper():投影轉換IEnumerable:使用 C# 2.0 泛型關鍵字 from, where, orderby, select 為 LINQ 保留字(IDE 顯示藍色)。
傳統做法:組合 SQL 字串 → Command/Adapter 送資料庫。
LINQ:查詢語法直接整合在程式碼中,編譯期型別檢查,IntelliSense 支援。
篩選長度為 5 的名稱,轉大寫並排序:
BURKE CONNOR FRANK HARRIS DAVID
記憶體陣列查詢無法展現 LINQ 強大功能,針對資料庫的 DLINQ 才具實務意義。後續章節將逐步介紹 DLINQ 語法。