VB.Net program to demonstrate the TakeWhile() LINQ extension method

Here, we are going to demonstrate the TakeWhile() LINQ extension method in VB.Net.
Submitted by Nidhi, on February 08, 2021 [Last updated : March 08, 2023]

VB.Net – TakeWhile() LINQ Extension Method

In this program, we will use TakeWhile() LINQ extension method. This method is used to get records till the specified condition is true.

Program/Source Code:

The source code to demonstrate the TakeWhile() LINQ extension method is given below. The given program is compiled and executed successfully.

VB.Net code to demonstrate the example of TakeWhile() LINQ extension method

'VB.NET program to demonstrate the 
'TakeWhile() LINQ extension method.

Imports System
Imports System.IO
Imports System.Linq

Module Module1
    Public Class Employee
        Public id As Integer
        Public name As String
        Public age As Integer
        Public salary As Integer
    End Class

    Sub Main()
        Dim empList = New List(Of Employee) From
        {
            New Employee() With {.id = 101, .name = "Amit", .age = 21, .salary = 5000},
            New Employee() With {.id = 102, .name = "Arun", .age = 22, .salary = 5500},
            New Employee() With {.id = 103, .name = "Aman", .age = 23, .salary = 6000},
            New Employee() With {.id = 104, .name = "Amar", .age = 21, .salary = 6700},
            New Employee() With {.id = 105, .name = "Akki", .age = 26, .salary = 7500},
            New Employee() With {.id = 105, .name = "Anuj", .age = 23, .salary = 8700}
        }

        Dim Result = empList.TakeWhile(Function(e) e.salary < 7000)


        Console.WriteLine("Employees: ")
        For Each emp In Result
            Console.WriteLine(emp.id & " " & emp.name & " " & emp.age & " " & emp.salary)
        Next
    End Sub
End Module

Output

Employees:
101 Amit 21 5000
102 Arun 22 5500
103 Aman 23 6000
104 Amar 21 6700
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains Employee class and a Main() function. The Employee class contains the data member id, name, age, and salary.

The Main() function is the entry point for the program. In the Main() function, we created a list of employees and then get the employee records in the collection till the specified condition is true using TakeWhile() LINQ extension method and after that, we printed the result on the console screen.

VB.Net LINQ Query Programs »






Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.