Home »
VB.Net »
VB.Net Programs
VB.Net program to demonstrate the LINQ Any() extension method with Function() to check an array contains a negative number or not
By Nidhi Last Updated : November 11, 2024
VB.Net – LINQ Any() extension method with Function()
In this program, we will use check an array of integers containing a negative number or not using LINQ Any() extension method.
Program/Source Code:
The source code to demonstrate the LINQ Any() extension method with Function() to check an array contains a negative number or not is given below. The given program is compiled and executed successfully.
VB.Net code to check an array contains a negative number or not
'VB.NET program to demonstrate the LinQ Any() extension
'method with Function() to check array contains a
'negative number or not.
Imports System
Imports System.IO
Imports System.Linq
Module Module1
Sub Main()
Dim intVals() As Integer = {50, 20, 40, -35, 10, 27, 60}
Dim neg = intVals.Any(Function(val As Integer)
Return val < 0
End Function)
If neg = True Then
Console.WriteLine("Array contains negative number")
Else
Console.WriteLine("Array does not contain negative number")
End If
End Sub
End Module
Output
Array contains negative number
Press any key to continue . . .
Explanation
In the above program, we created a module Module1 that contains the Main() function. The Main() function is the entry point for the program.
In the Main() function, we created an integer array that contains 7 elements. Here, we checked an array that contains a negative number or not using Any() extension method and print the appropriate message on the console screen.
VB.Net LINQ Query Programs »