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.

C# 8.0 and .NET Core 3.0 – Modern Cross-Platform Development: Build applications with C#, .NET Core, Entity Framework Core, ASP.NET Core, and ML.NET using Visual Studio Code, 4th Edition
ASP.NET Core 3 and Angular 9: Full stack web development with .NET Core 3.1 and Angular 9, 3rd Edition
C# in Depth, 4th Edition

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