Linq Concat Method in C#

Linq Concat Method in C# with Examples

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

  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

In the next article, I am going to discuss the Ordering Operators in LINQ with examples. In this article, I try to explain the Linq Concat Method in C# as well as the Difference Between Concat and Union Operator in C# with examples. I hope you understood the need and use of the Concat operator in Linq.

Leave a Reply

Your email address will not be published. Required fields are marked *