Concat and Cast operator Linq in VB.NET

This article defines the basic use of the Concat and Cast operator and also defines the difference between Concat and Union operator in LINQ.
  • 3861

Concat Operator

The Concat operator is similar to union but only difference that Union returns distinct values - duplicate rows removed, while concat selects all the values and doesn't remove any duplicates.  The Concat operator concatenates two sequences. It returns all elements from the first sequence, then all elements from the second sequence.

For example

Module Module1

    Sub Main()

        Dim seq1 As Integer() = {1, 2, 3, 6, 8}

        Dim seq2 As Integer() = {3, 4, 5}

        Dim concat As IEnumerable(Of Integer) = seq1.Concat(seq2)

        For Each i As Integer In concat

            Console.WriteLine(i)

        Next

    End Sub

End Module

OUTPUT

concat.gif
 

Cast operator

The Cast operator casts the elements of a sequence to a given type.

For example

Module Module1

    Sub Main()

        Dim array1 As New ArrayList()

        array1.AddRange(New Integer() {1, 2, 3, 4, 5})

        Dim sequence1 As IEnumerable(Of Integer) = array1.Cast(Of Integer)()

        For Each i As Integer In sequence1

            Console.WriteLine(i)

        Next

    End Sub

End Module

OUTPUT

c123.gif
 

Difference between Concat and Union operator

The Concat operator is similar to union but only difference that Union returns distinct values duplicate rows removed, while concat selects all the values and doesn't remove any duplicates.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.