This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Defines the result of a query.
Select [ var1 = ] fieldName1 [, [ var2 = ] fieldName2 [...] ]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.
You can use theSelect 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. TheSelect clause is not required for a query. If noSelect 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, seeAnonymous Types. When a query creates a named type, it will return a result of typeIEnumerable<T> whereT is the created type.
TheSelect clause can reference any variables in the current scope. This includes range variables identified in theFrom clause (orFrom clauses). It also includes any new variables created with an alias by theAggregate,Let,Group By, orGroup Join clauses, or variables from a previousSelect clause in the query expression. TheSelect clause can also include static values. For example, the following code example shows a query expression in which theSelect clause defines the query result as a new anonymous type with four members:ProductName,Price,Discount, andDiscountedPrice. TheProductName andPrice member values are taken from the product range variable that is defined in theFrom clause. TheDiscountedPrice member value is calculated in theLet clause. TheDiscount member is a static value.
' 10% discount Dim discount_10 = 0.1Dim priceList = From product In products Let DiscountedPrice = product.UnitPrice * (1 - discount_10) Select product.ProductName, Price = product.UnitPrice, Discount = discount_10, DiscountedPriceTheSelect clause introduces a new set of range variables for subsequent query clauses, and previous range variables are no longer in scope. The lastSelect 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 firstSelect clause identifies the range variables for theWhere clause and the secondSelect clause. The secondSelect 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, OrderIDIf theSelect clause identifies a single item to return, the query expression returns a collection of the type of that single item. If theSelect 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 theSelect clause. The first query returns a collection of company names as strings. The second query returns a collection ofCustomer objects populated with the company names and address information.
Dim customerNames = From cust In customers Select cust.CompanyNameDim 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}The following query expression uses aFrom clause to declare a range variablecust for thecustomers collection. TheSelect clause selects the customer name and ID value and populates theCompanyName andCustomerID columns of the new range variable. TheFor Each statement loops over each returned object and displays theCompanyName andCustomerID 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) NextEnd SubWas this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?