標準查詢運算子概觀

「標準查詢運算子」(Standard Query Operator) 是形成 Language-Integrated Query (LINQ) 模式的方法。 這些方法大部分都可用於序列,而這裡的序列是指其型別會實作 IEnumerable<T> 介面或 IQueryable<T> 介面的物件。 標準查詢運算子提供的查詢功能包括篩選、投影、彙總 (Aggregation)、排序等等。

LINQ 標準查詢運算子共有兩組,其中一組適用於型別 IEnumerable<T> 的物件,而另一組則適用於型別 IQueryable<T> 的物件。 構成各組的方法分別隷屬於 EnumerableQueryable 類別的靜態 (Static) 成員。 這些方法會定義為其適用型別的「擴充方法」(Extension Method)。 這表示您可以使用靜態方法語法或執行個體方法 (Instance Method) 語法來呼叫這些方法。

此外,還有幾個標準查詢運算子方法適用於不是以 IEnumerable<T>IQueryable<T> 做為依據的其他型別。 Enumerable 型別定義兩個這類方法,而這兩個方法都適用於型別 IEnumerable 的物件。 這些方法 (Cast<TResult>(IEnumerable)OfType<TResult>(IEnumerable)) 可以讓您啟用以 LINQ 模式查詢非參數型或非泛型集合的功能。 採用的方法是建立物件的強型別集合。 Queryable class 定義兩個類似的方法,即 Cast<TResult>(IQueryable)OfType<TResult>(IQueryable),適用於型別 Queryable 的物件。

標準查詢運算子的執行時機並不相同,實際情況需視其傳回的是單一值或值序列而定。 傳回單一值的方法 (例如 AverageSum) 會立即執行; 而傳回序列的方法則會延後執行查詢,並傳回可列舉的物件。

若以適用於記憶體中集合的方法 (亦即擴充 IEnumerable<T> 的方法) 為例,傳回的可列舉物件會擷取傳遞至方法的引數。 在列舉該物件時,將會採用查詢運算式的邏輯並傳回查詢結果。

相反地,擴充 IQueryable<T> 的方法不會實作任何查詢行為,而會建置 (Build) 表示要執行之查詢的運算式樹狀架構。 查詢的工作是由來源 IQueryable<T> 物件負責處理。

查詢方法的呼叫可以共同鏈結成一項查詢,讓查詢變得相當複雜。

下列程式碼範例示範如何使用標準查詢運算子取得序列的相關資訊。

        Dim sentence = "the quick brown fox jumps over the lazy dog"
        ' Split the string into individual words to create a collection.
        Dim words = sentence.Split(" "c)

        Dim query = From word In words 
                    Group word.ToUpper() By word.Length Into gr = Group 
                    Order By Length _
                    Select Length, GroupedWords = gr

        Dim output As New System.Text.StringBuilder
        For Each obj In query
            output.AppendLine(String.Format("Words of length {0}:", obj.Length))
            For Each word As String In obj.GroupedWords
                output.AppendLine(word)
            Next
        Next

        'Display the output
        MsgBox(output.ToString())

        ' This code example produces the following output:
        '
        ' Words of length 3:
        ' THE
        ' FOX
        ' THE
        ' DOG
        ' Words of length 4:
        ' OVER
        ' LAZY
        ' Words of length 5:
        ' QUICK
        ' BROWN
        ' JUMPS 

           string sentence = "the quick brown fox jumps over the lazy dog";
           // Split the string into individual words to create a collection.
           string[] words = sentence.Split(' ');

           // Using query expression syntax.
           var query = from word in words
                       group word.ToUpper() by word.Length into gr
                       orderby gr.Key
                       select new { Length = gr.Key, Words = gr };

           // Using method-based query syntax.
           var query2 = words.
               GroupBy(w => w.Length, w => w.ToUpper()).
               Select(g => new { Length = g.Key, Words = g }).
               OrderBy(o => o.Length);

           foreach (var obj in query)
           {
               Console.WriteLine("Words of length {0}:", obj.Length);
               foreach (string word in obj.Words)
                   Console.WriteLine(word);
           }

           // This code example produces the following output:
           //
           // Words of length 3:
           // THE
           // FOX
           // THE
           // DOG
           // Words of length 4:
           // OVER
           // LAZY
           // Words of length 5:
           // QUICK
           // BROWN
           // JUMPS 

查詢運算式語法

某些較常用的標準查詢運算子具有專屬的 C# 和 Visual Basic 語言關鍵字語法,使系統可以在「查詢運算式」(Query Expression) 中一併呼叫這些運算子。 如需具有專屬關鍵字之標準查詢運算子及其對應語法的詳細資訊,請參閱標準查詢運算子的查詢運算式語法

擴充標準查詢運算子

您可以透過建立適用於目標網域或技術的網域特定方法,擴充標準查詢運算子的集合。 您也可以將標準查詢運算子取代成自己的實作,以提供額外的服務,例如遠端評估、查詢轉譯以及最佳化。 如需範例,請參閱 AsEnumerable<TSource>

相關章節

您可以利用下列連結,檢視依據功能提供各種標準查詢運算子相關詳細資訊的說明主題。

排序資料

設定作業

篩選資料

數量詞作業

投影作業

分割資料

聯結作業

分組資料

產生作業

相等比較作業

項目作業

轉換資料型別

串連作業

彙總作業

請參閱

參考

Enumerable

Queryable

擴充方法 (C# 程式設計手冊)

概念

LINQ 查詢簡介 (C#)

標準查詢運算子的查詢運算式語法

依據執行方式將標準查詢運算式分類

擴充方法 (Visual Basic)