LINQ Concat Method

  Total Views : 168
  Comments : 0
 
  Keywords : How to use linq concactenation method in c#, vb.net with example, Linq concatenation operation in c# / vb.net to contact two collection sequences to single collection with example

Here we will learn linq concatenation method in c#, vb.net with example and how to use linq concat operator to contact or combine multiple collection sequences into single collection in c#, vb.net with example.

LINQ Concat Method

In LINQ concat method or operator is used to concatenate or append two collection elements into single collection and it does not remove duplicates from the two sequences.

 

Following is the pictorial representation of LINQ Concat method.

 

LINQ Concatenation Method with Example

 

In above diagram two list elements combined into single collection.

Syntax of LINQ Concat Method

Following is the syntax of using LINQ Concat method to combine two sequences into single sequence.

 

C# Code

 

var result = arr1.Concat(arr2);

VB.NET Code

 

Dim result = arr1.Concat(arr2)

If you observe above syntax we are concatenating two lists into single list.

Example of LINQ Concat Method

Following is the example of LINQ Concat method.

 

C# Code

 

using System;

using System.Linq;

namespace Linqtutorials

{

class Program

{

static void Main(string[] args)

{

string[] arr1 = { "a", "b", "c", "d" };

string[] arr2 = { "c", "d", "e", "f" };

var result = arr1.Concat(arr2);

foreach (var item in result)

{

Console.WriteLine(item);

}

Console.ReadLine();

}

}

}

VB.NET Code

 

Module Module1

Sub Main()

Dim arr1 As String() = {"a", "b", "c", "d"}

Dim arr2 As String() = {"c", "d", "e", "f"}

Dim result = arr1.Concat(arr2)

For Each item In result

Console.WriteLine(item)

Next

Console.ReadLine()

End Sub

End Module

If you observe above example we are concatenating two sequences “arr1”, “arr2” into single sequence using LINQ Concat method.

Output of LINQ Contact Method Example

Following is the result of LINQ Concat method example.

 

a

b

c

d

c

d

e

f

This is how we can use LINQ concat method to combine multiple sequences into single sequence in c#, vb.net.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User