Union Operator LINQ using VB.NET

This article defines the basic use of union operator in LinQ using VB.NET.
  • 7582

Introduction

Union returns all the elements of the first sequence, followed by all the elements of the second, but removes any duplicates.

SQL Union

In SQL server The UNION operator is used to combine the result-set of two or more SELECT statements. The below example defines the two table which has the common date.

SELECT name FROM Table1
Union
SELECT name FROM Table2

LINQ Union

The below defines the num1 and num2 are an array of numbers or even objects. Here is an example of num1,num2.

Dim num1 As Integer() = {1, 2, 3, 4, 5, 6}

Dim num2 As Integer() = {3, 4, 5, 7, 8}

Using Union operator

Dim commonality As IEnumerable(Of Integer) = num1.Union(num2)

        For Each i As Integer In commonality

            Console.WriteLine(i)

        Next

For example

Module Module1

    Sub Main()

        Dim num1 As Integer() = {1, 2, 3, 4, 5, 6}

        Dim num2 As Integer() = {3, 4, 5, 7, 8}

        Dim commonality As IEnumerable(Of Integer) = num1.Union(num2)

        Console.WriteLine("Union of num1 and num2:")

        For Each i As Integer In commonality

            Console.WriteLine(i)

        Next

    End Sub

End Module

 

OUTPUT

union.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.