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

C# 3.0 查詢運算式完整指南:LINQ 語法、翻譯機制、標準查詢運算子與模式實作

     編輯:關於C語言

查詢運算式:語言整合查詢的宣告式語法

查詢運算式提供類 SQL 的宣告式語法,編譯器將其純語法轉譯為方法呼叫鏈,不綁定特定資料來源(記憶體集合、SQL、XML、自訂提供者)。

基本結構

from c in customers
where c.City == "London"
orderby c.Name
select new { c.Name, c.Phone }
  • from:定義範圍變數與資料來源
  • where:篩選條件
  • orderby:排序
  • select/group:投影或分組
  • into:查詢延續(子查詢)

翻譯機制:語法對應到標準查詢運算子

查詢子句對應方法說明
whereWhereLambda 述詞篩選
selectSelect投影轉換
group ... byGroupBy分組,產生 IGrouping<K,T>
orderbyOrderBy/ThenBy主要/次要排序
orderby ... descendingOrderByDescending/ThenByDescending遞減排序
多重 fromSelectMany扁平化/交叉聯結
into巢狀查詢前一查詢結果作為下一查詢來源

翻譯範例

Where + Select

from c in customers where c.City == "London" select c.Name
// 翻譯
customers.Where(c => c.City == "London").Select(c => c.Name)

GroupBy

from c in customers group c.Name by c.Country
// 翻譯
customers.GroupBy(c => c.Country, c => c.Name)

OrderBy + ThenBy

from c in customers orderby c.Country, c.Balance descending select c
// 翻譯
customers.OrderBy(c => c.Country).ThenByDescending(c => c.Balance)

SelectMany(多重 from)

from c in customers
from o in c.Orders
where o.Date.Year == 2005
select new { c.Name, o.Id }
// 翻譯
customers.SelectMany(c => c.Orders.Where(o => o.Date.Year == 2005)
    .Select(o => new { c.Name, o.Id }))

Into 查詢延續

from c in customers group c by c.Country into g
select new { Country = g.Key, Count = g.Count() }
// 翻譯
customers.GroupBy(c => c.Country)
    .Select(g => new { Country = g.Key, Count = g.Count() })

查詢運算式模式:自訂 LINQ 提供者的契約

型別實作下列方法即支援查詢運算式(可為實例方法或擴充方法):

delegate R Func<A, R>(A arg);

class C<T> {
    public C<T> Where(Func<T, bool> predicate);
    public C<S> Select<S>(Func<T, S> selector);
    public C<S> SelectMany<S>(Func<T, C<S>> selector);
    public O<T> OrderBy<K>(Func<T, K> keySelector);
    public O<T> OrderByDescending<K>(Func<T, K> keySelector);
    public C<G<K, T>> GroupBy<K>(Func<T, K> keySelector);
    public C<G<K, E>> GroupBy<K, E>(Func<T, K> keySelector, Func<T, E> elementSelector);
}

class O<T> : C<T> {
    public O<T> ThenBy<K>(Func<T, K> keySelector);
    public O<T> ThenByDescending<K>(Func<T, K> keySelector);
}

class G<K, T> {
    public K Key { get; }
    public C<T> Group { get; }
}
  • 參數可為 Func 或 Expression<Func>(Lambda 兩者皆可轉換)
  • 回傳型別設計確保 ThenBy 僅能接在 OrderBy 後
  • GroupBy 回傳擁有 Key/Group 的序列

標準查詢運算子

System.Linq.Enumerable 提供針對 IEnumerable<T> 的參考實作,涵蓋所有模式方法,並擴充大量實用運算子(Distinct、Join、Aggregate 等)。

正式翻譯規則(遞迴應用)

  1. q1 into x q2 → from x in (q1) q2
  2. 多生成器 from g1, g2 → from g1 from g2
  3. from x in e where f → from x in (e).Where(x => f)
  4. 多 from + orderby + select → 注入匿名型別收集排序鍵與結果,OrderBy/ThenBy 後再 Select 還原
  5. 多 from + select → (e).SelectMany(x => from ... select v)
  6. 單 from + select → (e).Select(x => v)(若 v==x 則簡化為 (e))
  7. 單 from + group → (e).GroupBy(x => g, x => v)
  8. 單 from + orderby + select/group → (e).OrderBy(...).Select/GroupBy(...)

查詢運算式是 C# 語言整合查詢的核心表達形式,理解其翻譯機制與模式契約,不僅能寫出高效 LINQ,更能建構自訂查詢提供者(如 ORM、雲端 API 封裝)。

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