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

LINQ 語法結構與核心程序集:從語法糖到標準查詢運算子

     編輯:關於C#

LINQ 查詢表達式結構圖解

查詢表達式由 from、where、orderby、select 等子句組成,編譯器將其翻譯為標準查詢運算子 (擴展方法) 調用。

基本範例

string[] words = { "adobe", "excel", "acer", "query", "async", "xianfen", "apple", "amazon", "player", "best", "wow" };

// 查詢語法
var result = from w in words
             where w.StartsWith("a")
             orderby w descending
             select w;

// 輸出: async, apple, amazon, adobe, acer

編譯器翻譯過程 (逆向演化)

// 1. 查詢語法 (語法糖)
var q1 = from w in words where w.StartsWith("a") orderby w descending select w;

// 2. 擴展方法鏈 (編譯器生成)
var q2 = words.Where(w => w.StartsWith("a")).OrderByDescending(w => w);

// 3. 靜態方法調用 (最原始形式)
var q3 = Enumerable.OrderByDescending(
            Enumerable.Where(words, w => w.StartsWith("a")), 
            w => w);

結論:查詢語法純粹是語法糖,運行時仍是 .NET 2.0 CLR,IL 完全一致。

相關核心程序集

程序集命名空間/類型用途
System.Core.dllSystem.Linq.Enumerable, IQueryable, IGrouping...LINQ to Objects 核心
System.Data.DataSetExtensions.dllDataTableExtensions, DataRowExtensionsLINQ to DataSet
System.Data.Linq.dllDataContext, Table<T>, IQueryableLINQ to SQL
System.Xml.Linq.dllXElement, XDocument, XAttributeLINQ to XML

統一測試數據源定義

public class Student { public int Id; public string Name; 
    public override string ToString() => $"Student ID:{Id},Name:{Name}"; }
public class Course  { public int Id; public string Name; 
    public override string ToString() => $"Course ID:{Id},Name:{Name}"; }
public class Score   { public int StudentId; public int CourseId; public double Value; 
    public override string ToString() => $"StudentID:{StudentId},CourseID:{CourseId},Score:{Value}"; }

public static class DataSource
{
    public static List<Student> Students { get; } = new List<Student> {
        new Student{Id=1,Name="Andy"}, new Student{Id=2,Name="Bill"},
        new Student{Id=3,Name="Cindy"}, new Student{Id=4,Name="Dark"} };
    
    public static List<Student> Students2 { get; } = new List<Student> {
        new Student{Id=5,Name="Erik"}, new Student{Id=6,Name="Frank"} };
    
    public static List<Course> Courses { get; } = new List<Course> {
        new Course{Id=1,Name="C語言"}, new Course{Id=2,Name="生物物理"},
        new Course{Id=3,Name="編譯原理"}, new Course{Id=4,Name="財務"},
        new Course{Id=5,Name="大學英語"} };
    
    public static List<Score> Scores { get; } = new List<Score> {
        new Score{StudentId=1,CourseId=1,Value=78}, new Score{StudentId=1,CourseId=2,Value=60},
        new Score{StudentId=1,CourseId=3,Value=92}, new Score{StudentId=1,CourseId=4,Value=85},
        new Score{StudentId=1,CourseId=5,Value=55.5}, new Score{StudentId=2,CourseId=1,Value=59},
        new Score{StudentId=2,CourseId=2,Value=78}, new Score{StudentId=2,CourseId=3,Value=86},
        new Score{StudentId=3,CourseId=2,Value=60}, new Score{StudentId=4,CourseId=4,Value=96},
        new Score{StudentId=5,CourseId=5,Value=78} };
}

查詢表達式完整子句對照

子句對應擴展方法說明
fromSelectMany (多 from) / 無 (單 from)指定數據源與範圍變量
whereWhere篩選條件
orderbyOrderBy / OrderByDescending排序,支援 ThenBy
selectSelect投影結果形狀
group byGroupBy分組
into續接上一步結果讓分組/連接結果繼續查詢
joinJoin / GroupJoin內連接/分組連接
letSelect (引入中間變量)在查詢中定義臨時變量

理解語法糖本質,能在除錯時直接看懂 IL/方法鏈,也能靈活選擇查詢語法或方法語法。

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