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 的每个成员,因为查询会循环访问 @no__t。A 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,则从 @no__t 中推断 @no__t 的类型。If 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.


@No__t-0 子句用于标识查询的源数据和用于引用源集合中的元素的变量。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 子句隐式联接集合,或者使用 @no__t 或 Group Join 子句显式联接集合。You can join collections implicitly by using the Select clause, or explicitly by using the Join or Group Join clauses. 作为替代方法,您可以在单个 @no__t 0 子句中指定多个范围变量和集合,其中每个相关范围变量和集合用逗号分隔开。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. 下面的代码示例显示 From 子句的语法选项。The 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

@No__t-0 子句定义查询的作用域,这与 @no__t 循环的作用域类似。The From clause defines the scope of a query, which is similar to the scope of a For loop. 因此,查询范围内的每个 @no__t 0 范围变量都必须具有唯一的名称。Therefore, each element range variable in the scope of a query must have a unique name. 因为您可以为查询指定多个 From 子句,所以后面的 @no__t 子句可以引用 @no__t 2 子句中的范围变量,也可以引用前面的 From 子句中的范围变量。Because 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. 例如,下面的示例演示一个嵌套的 @no__t 0 子句,其中第二个子句中的集合基于第一个子句中范围变量的属性。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:

  • 使用 @no__t 0 和 Select 子句隐式组合多个集合,或使用 Join 或 @no__t 子句显式合并多个集合。Combine 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 By 子句对结果进行排序。Sort the result by using the Order By clause.

  • 使用 Group By 子句将相似结果组合在一起。Group 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 和 @no__t 3 子句来标识要返回的结果的各部分。Identify parts of the result to return by using the Skip, Take, Skip While, and Take While clauses.


下面的查询表达式使用 From 子句为 @no__t 集合中的每个 Customer 对象声明一个范围变量 custThe following query expression uses a From clause to declare a range variable cust for each Customer object in the customers collection. @No__t-0 子句使用范围变量将输出限制为指定区域的客户。The Where clause uses the range variable to restrict the output to customers from the specified region. @No__t-0 循环显示查询结果中每个客户的公司名称。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