from 子句 (C# 參考)

更新:2007 年 11 月

查詢運算式必須以 from 子句開頭。此外,查詢運算式可以包含子查詢,而子查詢也以 from 子句開頭。from 子句會指定下列各項:

  • 在其上執行查詢或子查詢的資料來源。

  • 區域「範圍變數」(Range Variable),代表來源序列中的每個項目。

範圍變數和資料來源都是強型別。from 子句中參考的資料來源的型別必須是 IEnumerableIEnumerable<T> 或衍生的型別,例如 IQueryable<T>

在下列範例中,numbers 是資料來源而 num 是範圍變數。請注意,變數都是強型別,即使使用 var 關鍵字也一樣。

class LowNums
{
    static void Main()
    {   
        // A simple data source.
        int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

        // Create the query.
        // lowNums is an IEnumerable<int>
        var lowNums = from num in numbers
            where num < 5
            select num;

        // Execute the query.
        foreach (int i in lowNums)
        {
            Console.Write(i + " ");
        }
    }        
}
// Output: 4 1 3 2 0

範圍變數

編譯器會在資料來源實作 IEnumerable<T> 時推斷範圍變數的型別。例如,如果來源的型別是 IEnumerable<Customer>,則範圍變數會推斷為 Customer。您必須明確指定型別的唯一時機是當來源為非泛型 IEnumerable 型別 (例如 ArrayList) 時。如需詳細資訊,請參閱HOW TO:使用 LINQ 查詢 ArrayList

在上述範例中,num 是推斷為型別 int。因為範圍變數是強型別,所以您可以在其上呼叫方法或在其他作業中使用它。例如,不撰寫 select num,您可以撰寫 select num.ToString() 讓查詢運算式傳回字串序列而非整數序列。或者可以撰寫 select n + 10 讓運算式傳回序列 14、11、13、12、10。如需詳細資訊,請參閱 select 子句 (C# 參考)

範圍變數如同 foreach 陳述式中的反覆運算變數,只有一個非常重要的差異:範圍變數永不實際儲存來源的資料。它只是一種語法上便於使用的工具,讓查詢描述執行查詢時會發生的事件。如需詳細資訊,請參閱LINQ 查詢簡介

從子句複合

在某些情況下,來源序列中的每個項目本身就是序列或包含序列。例如,您的資料來源可能是 IEnumerable<Student>,其中序列中的每個學生物件包含考試分數的清單。若要存取每個 Student 項目的內部清單,您可以使用複合 from 子句。這個技術如同使用巢狀 foreach 陳述式。您可以將 whereorderby 子句加入至 from 子句以篩選結果。下列範例顯示 Student 物件的序列,每個序列都包含代表考試分數的整數內部 List。若要存取內部清單,請使用複合 from 子句。您可以在必要時於兩個 from 子句之間插入子句。

class CompoundFrom
{
    // The element type of the data source.
    public class Student
    {
        public string LastName { get; set; }
        public List<int> Scores {get; set;}
    }

    static void Main()
    {

        // Use a collection initializer to create the data source. Note that 
        // each element in the list contains an inner sequence of scores.
        List<Student> students = new List<Student>
        {
           new Student {LastName="Omelchenko", Scores= new List<int> {97, 72, 81, 60}},
           new Student {LastName="O'Donnell", Scores= new List<int> {75, 84, 91, 39}},
           new Student {LastName="Mortensen", Scores= new List<int> {88, 94, 65, 85}},
           new Student {LastName="Garcia", Scores= new List<int> {97, 89, 85, 82}},
           new Student {LastName="Beebe", Scores= new List<int> {35, 72, 91, 70}} 
        };        

        // Use a compound from to access the inner sequence within each element.
        // Note the similarity to a nested foreach statement.
        var scoreQuery = from student in students
                         from score in student.Scores
                            where score > 90
                            select new { Last = student.LastName, score };

        // Execute the queries.
        Console.WriteLine("scoreQuery:");
        foreach (var student in scoreQuery)
        {
            Console.WriteLine("{0} Score: {1}", student.Last, student.score);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }       
}
/*
scoreQuery:
Omelchenko Score: 97
O'Donnell Score: 91
Mortensen Score: 94
Garcia Score: 97
Beebe Score: 91
*/

使用多個 from 子句執行聯結

複合 from 子句是用於存取單一資料來源中的內部集合。但是,查詢也可以包含多個 from 子句,從獨立資料來源產生補充查詢。此技術可以讓您執行特定類型的聯結作業,這些作業是無法使用 join 子句來完成。

下列範例顯示如何使用兩個 from 子句,形成兩個資料來源完整的交叉聯結。

class CompoundFrom2
{
    static void Main()
    {              
        char[] upperCase = { 'A', 'B', 'C'};
        char[] lowerCase = { 'x', 'y', 'z'};

        var joinQuery1 =
            from upper in upperCase
            from lower in lowerCase
                select new { upper, lower};

        var joinQuery2 =
            from lower in lowerCase
            where lower != 'x'
            from upper in upperCase
            select new { lower, upper };


        // Execute the queries.
        Console.WriteLine("Cross join:");
        foreach (var pair in joinQuery1)
        {
            Console.WriteLine("{0} is matched to {1}", pair.upper, pair.lower);
        }

        Console.WriteLine("Filtered non-equijoin:");
        foreach (var pair in joinQuery2)
        {
            Console.WriteLine("{0} is matched to {1}", pair.lower, pair.upper);
        }

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
/* Output:
        Cross join:
        A is matched to x
        A is matched to y
        A is matched to z
        B is matched to x
        B is matched to y
        B is matched to z
        C is matched to x
        C is matched to y
        C is matched to z
        Filtered non-equijoin:
        y is matched to A
        y is matched to B
        y is matched to C
        z is matched to A
        z is matched to B
        z is matched to C
        */

如需使用多個 from 子句之聯結作業的詳細資訊,請參閱 HOW TO:執行自訂聯結作業 (C# 程式設計手冊)

請參閱

概念

LINQ 查詢運算式 (C# 程式設計手冊)

其他資源

查詢關鍵字 (C# 參考)