Select Clause (Visual Basic)
Defines the result of a query.
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 Anonymous Types (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. For 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. 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.
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. For example, the following query returns the company name and order ID for every customer order for which the total exceeds 500. The first Select clause identifies the range variables for the Where clause and the second Select clause. The second Select clause identifies the values returned by the query as a new anonymous type.
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.
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.