Loading

Microsoft .Net 6 LINQ

What is Linq Concat Method in C# ?. The Complete Microsoft .Net 6 LINQ Developer Course 2022 [Videos].

In this Video, I am going to discuss the Linq Concat Method in C# with some examples. Please watch our previous Video before proceeding to this Video where we discussed the Linq Union Method in C# with some examples. As part of this Video, we are going to discuss the following pointers. What is the Concat Method in Linq?

  1. What is the Concat Method in Linq?
  2. Why do we need to use the Concat Method?
  3. Examples using both Query and Method Syntax.
  4. What are the differences between Concat and union operators in Linq? 

Linq Concat Method in C#:

The Linq Concat Method in C# is used to concatenate two sequences into one sequence. There is only one version available for this method whose signature is given below.

Linq Cancat Method in C# with Examples

Example1:

In the following example, we have created two integer sequences and then concatenate two sequences into one sequence using the Concat operator.

using System.Linq;
using System;
using System.Collections.Generic;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> sequence1 = new List<int> { 1, 2, 3, 4 };
List<int> sequence2 = new List<int> { 2, 4, 6, 8 };
var result = sequence1.Concat(sequence2);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}

Output:

Concat Operator in Linq Output

If you notice in the above output then you will see that the duplicate elements are not removed. Now let us concatenate the above two sequences using the Union operator and observe what happened.

Concatenate using Union Operator:

In the below example, we concatenate the two integer sequences into one sequence using the Linq Union Operator.

using System.Linq;
using System;
using System.Collections.Generic;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> sequence1 = new List<int> { 1, 2, 3, 4 };
List<int> sequence2 = new List<int> { 2, 4, 6, 8 };
var result = sequence1.Union(sequence2);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}

Output:

Union Operator in Linq Output

If you observe in the above output, then you will see that the duplicate elements are removed from the result set.

What is the difference between Concat and Union operators in Linq?

The Concat operator is used to concatenate two sequences into one sequence without removing the duplicate elements. That means it simply returns the elements from the first sequence followed by the elements from the second sequence. 

On the other hand, the Linq Union operator is also used to concatenate two sequences into one sequence by removing the duplicate elements. 

Note: While working with the Concat operator if any of the sequences is null then it will throw an exception.

Example2:

In the following example, the second sequence is null and while performing the concatenate operation using the concat operator it will throw an exception.

using System.Linq;
using System;
using System.Collections.Generic;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> sequence1 = new List<int> { 1, 2, 3, 4 };
List<int> sequence2 = null;
var result = sequence1.Concat(sequence2);
foreach (var item in result)
{
Console.WriteLine(item);
}
Console.ReadLine();
}
}
}

Now run the application and you will get the following exception.

Argument Null Exception in Linq Concat Operator

See All

Comments (467 Comments)

Submit Your Comment

See All Posts

Related Posts

Microsoft .Net 6 LINQ / Blog

What is Microsoft .Net 6 LINQ?

LINQ stands for Language-Integrated Query and it is a powerful query language that was introduced with .Net 3.5 & Visual Studio 2008. You can use LINQ with C# or VB to query different types of data sources such as SQL, XML, In memory objects, etc.
14-Feb-2022 /38 /467

Microsoft .Net 6 LINQ / Blog

What is Architecture of LINQ?

In this article, I am going to discuss the Architecture of LINQ. The term LINQ stands for Language Integrated Query and it is pronounced as LINK. Nowadays the use of use LINQ increasing rapidly. So, as a developer, you should understand the Linq and its architecture. At the end of this article, you will have a very good understanding of the following pointers.
14-Feb-2022 /38 /467

Microsoft .Net 6 LINQ / Blog

How to write Different Ways to Write LINQ Query?

In this article, I am going to discuss the Different Ways to write LINQ Query i.e. Linq Query Syntax and Linq Method Syntax with examples. Please read our previous article where we discussed the Architecture of LINQ i.e. how LINQ works. In this article, we are going to discuss the following pointers.
14-Feb-2022 /38 /467