From 子句 (Visual Basic)From Clause (Visual Basic)

指定一个或多个范围变量以及要查询的集合。Specifies one or more range variables and a collection to query.


From element [ As type ] In collection [ _ ]  
  [, element2 [ As type2 ] In collection2 [, ... ] ]  


术语Term 定义Definition
element 必需。Required. 用于循环访问集合中的元素的范围变量A range variable used to iterate through the elements of the collection. collection当查询循环访问时,范围变量用于引用的每个成员 collectionA range variable is used to refer to each member of the collection as the query iterates through the collection. 必须为可枚举类型。Must be an enumerable type.
type 可选。Optional. element 的类型。The type of element. 如果未 type 指定,则 element 将从中推断出的类型 collectionIf no type is specified, the type of element is inferred from collection.
collection 必需。Required. 引用要查询的集合。Refers to the collection to be queried. 必须为可枚举类型。Must be an enumerable type.


From子句用于标识查询的源数据和用于引用源集合中的元素的变量。The From clause is used to identify the source data for a query and the variables that are used to refer to an element from the source collection. 这些变量称为范围变量These variables are called range variables. From对于查询,子句是必需的,除非 Aggregate 使用子句标识仅返回聚合结果的查询。The From clause is required for a query, except when the Aggregate clause is used to identify a query that returns only aggregated results. 有关详细信息,请参阅Aggregate 子句For more information, see Aggregate Clause.

您可以 From 在查询中指定多个子句,以标识要联接的多个集合。You can specify multiple From clauses in a query to identify multiple collections to be joined. 指定多个集合时,它们将被单独循环访问,如果相关,可以联接它们。When multiple collections are specified, they are iterated over independently, or you can join them if they are related. 您可以使用子句隐式联接集合 Select ,或使用 or 子句显式联接 Join 集合 Group JoinYou can join collections implicitly by using the Select clause, or explicitly by using the Join or Group Join clauses. 作为替代方法,您可以在单个子句中指定多个范围变量和集合 From ,其中每个相关范围变量和集合用逗号分隔开。As an alternative, you can specify multiple range variables and collections in a single From clause, with each related range variable and collection separated from the others by a comma. 下面的代码示例演示了子句的两种语法选项 FromThe following code example shows both syntax options for the From clause.

' Multiple From clauses in a query.
Dim result = From var1 In collection1, var2 In collection2

' Equivalent syntax with a single From clause.
Dim result2 = From var1 In collection1
              From var2 In collection2

From子句定义查询的作用域,该作用域类似于循环的作用域 ForThe From clause defines the scope of a query, which is similar to the scope of a For loop. 因此, element 查询作用域中的每个范围变量都必须具有唯一的名称。Therefore, each element range variable in the scope of a query must have a unique name. 由于可以为查询指定多个 From 子句,因此后续 From 子句可以引用子句中的范围变量 From ,或者可以引用上一个子句中的范围变量 FromBecause you can specify multiple From clauses for a query, subsequent From clauses can refer to range variables in the From clause, or they can refer to range variables in a previous From clause. 例如,下面的示例显示一个嵌套 From 子句,其中第二个子句中的集合基于第一个子句中范围变量的属性。For example, the following example shows a nested From clause where the collection in the second clause is based on a property of the range variable in the first clause.

Dim allOrders = From cust In GetCustomerList()
                From ord In cust.Orders
                Select ord

每个 From 子句可以后跟其他查询子句的任意组合,以优化查询。Each From clause can be followed by any combination of additional query clauses to refine the query. 可以通过以下方式优化查询:You can refine the query in the following ways:

  • 使用和子句隐式组合多个集合 From Select ,或使用 Join or 子句显式组合多个集合 Group JoinCombine multiple collections implicitly by using the From and Select clauses, or explicitly by using the Join or Group Join clauses.

  • 使用 Where 子句来筛选查询结果。Use the Where clause to filter the query result.

  • 使用子句对结果进行排序 Order BySort the result by using the Order By clause.

  • 使用子句将相似结果组合在一起 Group ByGroup similar results together by using the Group By clause.

  • 使用 Aggregate 子句标识要为整个查询结果计算的聚合函数。Use the Aggregate clause to identify aggregate functions to evaluate for the whole query result.

  • 使用 Let 子句引入一个迭代变量,该变量的值由表达式而不是集合确定。Use the Let clause to introduce an iteration variable whose value is determined by an expression instead of a collection.

  • 使用 Distinct 子句可忽略重复的查询结果。Use the Distinct clause to ignore duplicate query results.

  • 使用 SkipTakeSkip While 和子句标识要返回的结果的各个部分 Take WhileIdentify parts of the result to return by using the Skip, Take, Skip While, and Take While clauses.


下面的查询表达式使用 From 子句为 cust 集合中的每个对象声明一个范围变量 Customer customersThe following query expression uses a From clause to declare a range variable cust for each Customer object in the customers collection. Where子句使用范围变量将输出限制为指定区域的客户。The Where clause uses the range variable to restrict the output to customers from the specified region. For Each 循环将在查询结果中显示每个客户的公司名称。The For Each loop displays the company name for each customer in the query result.

Sub DisplayCustomersForRegion(ByVal customers As List(Of Customer),
                              ByVal region As String)

    Dim customersForRegion = From cust In customers
                             Where cust.Region = region

    For Each cust In customersForRegion
End Sub

另请参阅See also