from 子句(C# 参考)

查询表达式必须以 from 子句开头。 此外,查询表达式可包含也以 from 子句开头的子查询。 from 子句指定下列各项:

  • 将在其上运行查询或子查询的数据源。

  • 表示源序列中每个元素的本地范围变量

范围变量和数据源已强类型化。 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。 仅在以下情况下必须显式指定类型:源是 ArrayList 等非泛型 IEnumerable 类型时。 有关详细信息,请参阅如何使用 LINQ 查询 ArrayList

在以上示例中,num 推断为 int 类型。 由于强类型化了范围变量,所以可以在其上调用方法,或将其用于其他操作中。 例如,不再编写 select num,而编写 select num.ToString(),使查询表达式返回字符串序列,而不是整数序列。 或者可以编写 select num + 10,使表达式返回序列 14、11、13、12、10。 有关详细信息,请参阅 select 子句

范围变量与 foreach 语句中的迭代变量类似,一项非常重要的区别除外:范围变量从不实际存储来自源的数据。 它只是一种语法上的便利,让查询能够描述执行查询时将发生的情况。 有关详细信息,请参阅 LINQ 查询简介 (C#)

复合 from 子句

在某些情况下,源序列中的每个元素可能本身就是一个序列,或者包含一个序列。 例如,数据源可能是 IEnumerable<Student>,其中序列中的每个学生对象都包含测试分数的列表。 要访问每个 Student 元素的内部列表,可以使用复合 from 子句。 这种方法类似于使用嵌套的 foreach 语句。 可以向任一 from子句添加 whereorderby 子句筛选结果。 下面的示例演示 Student 对象的序列,其中每个对象都包含一个整数内部 List,表示测验分数。 访问内部列表可使用复合 from 子句。 如有必要,可以在这两个 from 子句间插入子句。

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

    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 Student {LastName="Omelchenko", Scores= [97, 72, 81, 60]},
           new Student {LastName="O'Donnell", Scores= [75, 84, 91, 39]},
           new Student {LastName="Mortensen", Scores= [88, 94, 65, 85]},
           new Student {LastName="Garcia", Scores= [97, 89, 85, 82]},
           new Student {LastName="Beebe", Scores= [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:");
        // Rest the mouse pointer on scoreQuery in the following line to
        // see its type. The type is IEnumerable<'a>, where 'a is an
        // anonymous type defined as new {string Last, int score}. That is,
        // each instance of this anonymous type has two members, a string
        // (Last) and an int (score).
        foreach (var student in scoreQuery)
        {
            Console.WriteLine("{0} Score: {1}", student.Last, student.score);
        }
    }
}
/*
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'];

        // The type of joinQuery1 is IEnumerable<'a>, where 'a
        // indicates an anonymous type. This anonymous type has two
        // members, upper and lower, both of type char.
        var joinQuery1 =
            from upper in upperCase
            from lower in lowerCase
            select new { upper, lower };

        // The type of joinQuery2 is IEnumerable<'a>, where 'a
        // indicates an anonymous type. This anonymous type has two
        // members, upper and lower, both of type char.
        var joinQuery2 =
            from lower in lowerCase
            where lower != 'x'
            from upper in upperCase
            select new { lower, upper };

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

        Console.WriteLine("Filtered non-equijoin:");
        // Rest the mouse pointer over joinQuery2 to verify its type.
        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 子句的联接操作,请参阅执行左外部联结

另请参阅