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

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

VB.Net – Intersect() LINQ Extension Method

In this program, we will use Intersect() method. This method requires two collections. It returns a new collection with common elements of both collections.

Program/Source Code:

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

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

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

Module Module1
    Sub Main()
        Dim intList1 = New List(Of Integer) From {101, 102, 103, 104, 105, 106}
        Dim intList2 = New List(Of Integer) From {201, 202, 103, 104, 205, 206}

        Dim result = intList1.Intersect(intList2)

        Console.WriteLine("Results:")
        For i = 0 To result.Count() - 1 Step 1
            Console.Write(result.ElementAt(i) & " ")
        Next
        Console.WriteLine()
    End Sub
End Module

Output

Results:
103 104
Press any key to continue . . .

Explanation

In the above program, we created a module Module1 that contains a Main() function. The Main() function is the entry point for the program.

In the Main() function, we created two integer collections with some elements. Then we used Intersect() method to get common elements from both collections and print them on the console screen.

VB.Net LINQ Query Programs »






Comments and Discussions!

Load comments ↻






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