×

VB.Net Programs

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

By Nidhi Last Updated : November 14, 2024

VB.Net – Union() LINQ Extension Method

In this program, we will use Union() method. This method requires two collections. It returns a new collection that includes distinct elements from both collections.

Program/Source Code:

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

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

'VB.NET program to demonstrate the 
'Union() 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.Union(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:
101 102 103 104 105 106 201 202 205 206
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 Union() method to get distinct 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.