Compartilhar via


Cláusula Select (Visual Basic)

Defines the result of a query.

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.

Comentários

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. The Select clause is not required for a query. 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 Tipos anônimos (Visual Basic). When a query creates a named type, it will return a result of type IEnumerable<T> where T is the created type.

The Select clause can reference any variables in the current scope. This includes range variables identified in the From clause (or From clauses). 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. The Select clause can also include static values. Por exemplo, o exemplo de código a seguir mostra uma expressão de consulta na qual o Select cláusula define o resultado da consulta como um novo tipo anônimo com quatro membros: ProductName, Price, Discount, and DiscountedPrice. The ProductName and Price member values are taken from the product range variable that is defined in the From clause. The DiscountedPrice member value is calculated in the Let clause. 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

The Select clause introduces a new set of range variables for subsequent query clauses, and previous range variables are no longer in scope. The last Select clause in a query expression determines the return value of the query. Por exemplo, a seguinte consulta retorna a empresa nome e a ordem de ID para cada pedido do cliente para o qual o total exceder 500. O primeiro Select cláusula identifica as variáveis de intervalo para o Where e a segunda cláusula Select cláusula. 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

If the Select clause identifies a single item to return, the query expression returns a collection of the type of that single item. 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. 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. 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}

Exemplo

The following query expression uses a From clause to declare a range variable cust for the customers collection. The Select clause selects the customer name and ID value and populates the CompanyName and CustomerID columns of the new range variable. 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

Consulte também

Referência

A partir da cláusula (Visual Basic)

Clúasula Where (Visual Basic)

Cláusula Order By (Visual Basic)

Conceitos

Introdução ao LINQ no Visual Basic

Tipos anônimos (Visual Basic)

Outros recursos

Consultas(Visual Basic)