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

指定一個或多個範圍變數,以及要查詢的集合。Specifies one or more range variables and a collection to query.

語法Syntax

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

組件Parts

詞彙Term 定義Definition
element 必要項。Required. 用來逐一查看集合元素的範圍變數A range variable used to iterate through the elements of the collection. 範圍變數是用來在查詢逐一查看 collection 時,參考 @no__t 0 的每個成員。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,則會從 collection 推斷 element 的類型。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.

備註Remarks

@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. 除非使用 Aggregate 子句來識別只傳回匯總結果的查詢,否則查詢需要 From 子句。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.

您可以在查詢中指定多個 @no__t 0 子句,以識別要聯結的多個集合。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 子句,或使用 Join 或 @no__t 2 子句明確地聯結集合。You 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. 下列程式碼範例顯示 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 1 迴圈的範圍。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 1 子句可以參考 From 子句中的範圍變數,或參考前一個 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. 例如,下列範例會顯示一個 nested 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

每個 @no__t 0 子句後面都可以加上任何其他查詢子句的組合,以精簡查詢。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:

  • 使用 FromSelect 子句,或使用 JoinGroup Join 子句明確地結合多個集合。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 WhileTake While 子句,識別要傳回的結果部分。Identify parts of the result to return by using the Skip, Take, Skip While, and Take While clauses.

範例Example

下列查詢運算式會使用 From 子句,針對 customers 集合中的每個 @no__t 2 物件,宣告範圍變數 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
    Console.WriteLine(cust.CompanyName)
  Next
End Sub

另請參閱See also