VB.Net Predicate lambda

A predicate lambda in VB.NET is a lambda expression that returns a Boolean value. Predicate lambdas can be used to filter collections, to test conditions, and to perform other operations that require a Boolean value.

To create a predicate lambda, you use the Function keyword followed by the parameter list and the return type. The body of the lambda expression should contain the code that you want to execute to determine the Boolean result.

For example, the following code shows a predicate lambda that returns True if the input value is greater than or equal to 10:

Dim isGreaterThanOrEqualTo10 As Predicate(Of Integer) = Function(value As Integer) value >= 10

Predicate Delegate

A Predicate is a delegate that represents a method that takes one parameter and returns a Boolean value. Predicates are often used for filtering and testing conditions in collections. You can use lambda expressions to create concise and expressive Predicate functions.

Using Predicate Delegate

You can create a Predicate using a delegate and use it to filter a collection.

' Define a Predicate delegate Dim isEven As Predicate(Of Integer) = Function(x) x Mod 2 = 0 ' Create a list of integers Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ' Use Predicate to filter even numbers Dim evenNumbers As List(Of Integer) = numbers.FindAll(isEven)

In this example, we defined a Predicate isEven that checks if an integer is even. We then used FindAll to filter even numbers from the list.

Using Lambda Expressions

Lambda expressions simplify Predicate creation by providing a concise way to define the function inline:

Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ' Use a lambda expression as a Predicate to filter even numbers Dim evenNumbers As List(Of Integer) = numbers.FindAll(Function(x) x Mod 2 = 0)

Here, we used a lambda expression directly as a Predicate to filter even numbers. This is more readable and avoids defining a separate delegate.

Predicate lambdas can also be used to test conditions. For example, the following code shows how to use a predicate lambda to test if a number is even:

Dim isEven As Predicate(Of Integer) = Function(value As Integer) value Mod 2 = 0 ' Test if the number 10 is even. Dim is10Even As Boolean = isEven(10) ' Display the result. Console.WriteLine(is10Even) ' Output: True

Using Predicates with LINQ

Predicates are commonly used with LINQ for querying and filtering collections.

Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} ' Use LINQ with a lambda expression as a Predicate to filter even numbers Dim evenNumbers = From n In numbers Where Function(x) x Mod 2 = 0

Here, we used a lambda expression within a LINQ query to filter even numbers from the collection.

Conclusion

Predicates and lambda expressions provide a powerful way to filter and conditionally process data in VB.NET, making your code more concise and expressive. They are commonly used in scenarios where you need to filter collections or perform conditional checks.