Let keyword in LINQ using VB.NET

Here we will see that how to declare a temporary variable inside the LinQ Query using Let keyword.
  • 4604
Download Files:
 

The Let Keyword

LINQ has a keyword let that allow one to create temporary variable inside the query and perform the required action. We can assign the result of manipulation to temporary variable inside query and we can use that temporary variable to another manipulation.

Using Let keyword

Taking two two integer array that is Arrayfirst, arraysecond.

Dim Arrayfirst As Integer() = New Integer() {1, 2, 3}

        Dim Arraysecond As Integer() = New Integer() {1, 2, 3}

Now using Let keyword to find square value which are greater then 4.

Dim Squares = From Queryfirst In Arrayfirst From Querysecond In Arraysecond

           Let TheSquare = Queryfirst * Querysecond

           Where TheSquare > 4 Select Queryfirst, Querysecond, TheSquare

For example

Let's take a simple example of Linq query I am using two integer array to find square and after finding the square of the integer value I will use let keyword to find square value which are greater then 4.

Module Module1

    Sub Main()

        Dim Arrayfirst As Integer() = New Integer() {1, 2, 3}

        Dim Arraysecond As Integer() = New Integer() {1, 2, 3}

        Dim Squares = From Queryfirst In Arrayfirst From Querysecond In Arraysecond

           Let TheSquare = Queryfirst * Querysecond

           Where TheSquare > 4 Select Queryfirst, Querysecond, TheSquare

        For Each ThisSquare In Squares

            Console.WriteLine(ThisSquare.TheSquare.ToString())

        Next

    End Sub

End Module

OUTPUT

let.gif

Categories

More Articles

© 2018 DotNetHeaven. All rights reserved.