How to use LINQ Let clause in VB.NET

Web developers can use optional LINQ Let clause to compute values for each query result and reference them by using an alias. The alias can be used in other clauses, such as the Where clause. By using Let clause, Web developers can create a query statement that is easier to read because they can specify an alias for an expression clause included in the query and substitute the alias each time the expression clause is used.

Web developers can include any number of variable and expression assignments in the Let clause. Separate each assignment with a comma (,):

Let variable = expression [,…]

where

– Variable is an alias that can be used to reference the results of the supplied expression.

– Expression is an expression that will be evaluated and assigned to the specified variable.

Both are required.

The following example uses the Let clause to compute a 2.5 percent commission on product.

 

 

Dim commissionedProducts = From prod In products _

Let Commission = prod.UnitPrice * 1.025 _

Where Commission >= 1.5 _

Select prod.ProductName, prod.UnitPrice, Commission

 

For Each prod In commissionedProducts

Console.WriteLine(“Product: {0}, Price: {1}, _

Taxed Price: {2}”, prod.ProductName, _

prod.UnitPrice.ToString(“$#.00”), _

(prod.UnitPrice – prod. Commission).ToString(“$#.00”))

Next