VB.NET and Lambda – Filter elements from object collection with Logical Operators

Published on November 18, 2016 by abundantcode

Filed under VB.NET

Last modified November 18, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 626 times

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the where clause using Lambda in VB.NET. This sample gets all the employees who salary is greater than 20000 GBP and age is greater than 22.

How to Filter Employees List whose salary is greater than 20000 GBP and age is greater than 22 using Lambda in VB.NET ?

Namespace AbundantcodeVisualBasicSample
    Class Program
        Public Shared Sub Main(args As String())
            Dim employees As New List(Of Employee)()
            employees.Add(New Employee() With { _
                .Name = "Michael", _
                .Age = 55, _
                .Salary = 80000 _
            })
            employees.Add(New Employee() With { _
                 .Name = "Steve", _
                 .Age = 23, _
                 .Salary = 19000 _
            })
            employees.Add(New Employee() With { _
                 .Name = "John", _
                 .Age = 55, _
                 .Salary = 23000 _
            })
           ' Lambda expression demonstrating the usage of Logical operators in the Where clause.
            Dim output = employees.Where(Function(a) a.Salary > 20000 AndAlso a.Age > 22)

            Debug.WriteLine("Employees whose salary is greater than 20000 pounds and age > 22")
            For Each item As Employee In output
                Debug.WriteLine(item.Name)
            Next
        End Sub
    End Class
    Class Employee
        Public Property Name() As String
            Get
                Return m_Name
            End Get
            Set
                m_Name = Value
            End Set
        End Property
        Private m_Name As String
        Public Property Age() As Integer
            Get
                Return m_Age
            End Get
            Set
                m_Age = Value
            End Set
        End Property
        Private m_Age As Integer
        Public Property Salary() As Decimal
            Get
                Return m_Salary
            End Get
            Set
                m_Salary = Value
            End Set
        End Property
        Private m_Salary As Decimal
    End Class
End Namespace

Output

Employees whose salary is greater than 20000 pounds and age > 22

Michael

John

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C# and Lambda – Filter elements from object collection with Logical Operators

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the w...

VB.NET and LINQ – Filter elements from object collection with Logical Operators

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the w...

VB.NET and Lambda – Filter elements from object collection using where clause

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the w...