Select 子句 (Visual Basic)Select Clause (Visual Basic)

定義查詢的結果。Defines the result of a query.

語法Syntax

Select [ var1 = ] fieldName1 [, [ var2 = ] fieldName2 [...] ]  

組件Parts

var1
選擇性。Optional. 可以用來參考資料行運算式結果的別名。An alias that can be used to reference the results of the column expression.

fieldName1
必要項。Required. 要在查詢結果中傳回之欄位的名稱。The name of the field to return in the query result.

備註Remarks

您可以使用 Select 子句來定義要從查詢傳回的結果。You can use the Select clause to define the results to return from a query. 這可讓您定義由查詢所建立之新匿名型別的成員,或是以查詢所傳回之命名型別的成員為目標。This enables you to either define the members of a new anonymous type that is created by a query, or to target the members of a named type that is returned by a query. 查詢不需要 Select 子句。The Select clause is not required for a query. 如果未指定任何 Select 子句,則查詢會根據目前範圍所識別之範圍變數的所有成員,傳回型別。If no Select clause is specified, the query will return a type based on all members of the range variables identified for the current scope. 如需詳細資訊,請參閱匿名型別For more information, see Anonymous Types. 當查詢建立名為的型別時,它會傳回型別為 IEnumerable<T> 的結果,其中 T 是所建立的型別。When a query creates a named type, it will return a result of type IEnumerable<T> where T is the created type.

@No__t-0 子句可以參考目前範圍中的任何變數。The Select clause can reference any variables in the current scope. 這包括在 From 子句(或 From 子句)中識別的範圍變數。This includes range variables identified in the From clause (or From clauses). 它也包含以 AggregateLetGroup ByGroup Join 子句的別名建立的新變數,或查詢運算式中先前 Select 子句的變數。It also includes any new variables created with an alias by the Aggregate, Let, Group By, or Group Join clauses, or variables from a previous Select clause in the query expression. @No__t-0 子句也可以包含靜態值。The Select clause can also include static values. 例如,下列程式碼範例顯示一個查詢運算式,其中 Select 子句會將查詢結果定義為具有四個成員的新匿名型別: ProductNamePriceDiscountDiscountedPriceFor example, the following code example shows a query expression in which the Select clause defines the query result as a new anonymous type with four members: ProductName, Price, Discount, and DiscountedPrice. @No__t-0 和 @no__t 1 成員值取自在 From 子句中定義的產品範圍變數。The ProductName and Price member values are taken from the product range variable that is defined in the From clause. Let 子句中,會計算 @no__t 0 的成員值。The DiscountedPrice member value is calculated in the Let clause. @No__t 0 成員是靜態值。The Discount member is a static value.

' 10% discount 
Dim discount_10 = 0.1
Dim priceList =
  From product In products
  Let DiscountedPrice = product.UnitPrice * (1 - discount_10)
  Select product.ProductName, Price = product.UnitPrice,
  Discount = discount_10, DiscountedPrice

@No__t-0 子句為後續的查詢子句導入了一組新的範圍變數,而先前的範圍變數已不再在範圍內。The Select clause introduces a new set of range variables for subsequent query clauses, and previous range variables are no longer in scope. 查詢運算式中的最後一個 Select 子句會決定查詢的傳回值。The last Select clause in a query expression determines the return value of the query. 例如,下列查詢會傳回總計超過500的每個客戶訂單的公司名稱和訂單識別碼。For example, the following query returns the company name and order ID for every customer order for which the total exceeds 500. 第一個 Select 子句會識別 Where 子句和第二個 Select 子句的範圍變數。The first Select clause identifies the range variables for the Where clause and the second Select clause. 第二個 Select 子句會將查詢所傳回的值識別為新的匿名型別。The second Select clause identifies the values returned by the query as a new anonymous type.

Dim customerList = From cust In customers, ord In cust.Orders
                   Select Name = cust.CompanyName,
                          Total = ord.Total, ord.OrderID
                   Where Total > 500
                   Select Name, OrderID

如果 Select 子句識別要傳回的單一專案,則查詢運算式會傳回該單一專案之類型的集合。If the Select clause identifies a single item to return, the query expression returns a collection of the type of that single item. 如果 Select 子句識別要傳回的多個專案,則查詢運算式會根據選取的專案,傳回新匿名型別的集合。If the Select clause identifies multiple items to return, the query expression returns a collection of a new anonymous type, based on the selected items. 例如,下列兩個查詢會根據 Select 子句傳回兩個不同類型的集合。For example, the following two queries return collections of two different types based on the Select clause. 第一個查詢會以字串形式傳回公司名稱的集合。The first query returns a collection of company names as strings. 第二個查詢會傳回以公司名稱和位址資訊填入之 @no__t 0 物件的集合。The second query returns a collection of Customer objects populated with the company names and address information.

Dim customerNames = From cust In customers 
                    Select cust.CompanyName

Dim customerInfo As IEnumerable(Of Customer) =
  From cust In customers
  Select New Customer With {.CompanyName = cust.CompanyName,
                             .Address = cust.Address,
                             .City = cust.City,
                             .Region = cust.Region,
                             .Country = cust.Country}

範例Example

下列查詢運算式會使用 From 子句,針對 @no__t 2 集合宣告範圍變數 custThe following query expression uses a From clause to declare a range variable cust for the customers collection. @No__t-0 子句會選取 [客戶名稱] 和 [識別碼] 值,並填入新範圍變數的 CompanyName 和 @no__t 2 資料行。The Select clause selects the customer name and ID value and populates the CompanyName and CustomerID columns of the new range variable. @No__t-0 語句會在每個傳回的物件上迴圈,並顯示每一筆記錄的 CompanyName 和 @no__t 2 資料行。The For Each statement loops over each returned object and displays the CompanyName and CustomerID columns for each record.

Sub SelectCustomerNameAndId(ByVal customers() As Customer)
  Dim nameIds = From cust In customers
                Select cust.CompanyName, cust.CustomerID
  For Each nameId In nameIds
    Console.WriteLine(nameId.CompanyName & ": " & nameId.CustomerID)
  Next
End Sub

另請參閱See also