Intersect Operator in LINQ using VB.NET

This article defines the basic use of Intersect operator in LinQ using VB.NET.
  • 5492
 

Introduction

Intersect acts as an AND operator value is selected only if it appears in both statements. Intersect returns the elements that two sequences have in common.

SQL Intersect

The below example defines the two table which has the common date.

SELECT Date FROM Table1
INTERSECT
SELECT Date FROM Table2

LINQ Intersect

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 Intersect operator

Dim commonality As IEnumerable(Of Integer) = num1.Intersect(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.Intersect(num2)

        Console.WriteLine("common number are :")

        For Each i As Integer In commonality

            Console.WriteLine(i)

        Next

    End Sub

End Module

 

OUTPUT 

LINQ-Intersect-Operator-in-VB.NET.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.