Loading

Microsoft .Net 6 LINQ

What is LINQ Union in C#?. The Complete Microsoft .Net 6 LINQ Developer Course 2023 [Videos].

In this article, I am going to discuss the LINQ Union in C# with examples. Please read our previous article where we discussed the LINQ Intersect() Method with examples. As part of this article, we are going to discuss the following pointers related to the LINQ Union method in C#.

  1. What is LINQ Union in C#?
  2. Examples of C# LINQ Union Method using both Method and Query Syntax
  3. Using C# Linq Union Method with Complex Type.
  4. How to implement IEqualityComparer?
  5. Example using Anonymous Type
What us Linq Union in C#:

The LINQ Union Method in C# is used to combine the multiple data sources into one data source by removing the duplicate elements. There are two overloaded versions available for the LINQ Union Method as shown below.

LINQ Union Method in C# with Examples

Let us understand this with an example. Please have a look at the following image.

LINQ Union Method in C#

As shown in the above image, here we have two integer data sources i.e. DataSource 1 and Data Source 2. The DataSource 1 contains elements such as 1, 2, 3, 4, 5, 6 and the DataSource 2 contains elements such as 1, 3, 5, 8, 9, and 10. If we want to retrieve all the elements from both the collections by removing the duplicate element then we need to use the LINQ Union method.

Example1:

The following example shows the use of the LINQ Union() Method using both Method and Query Syntax to fetch all the elements from both the collections by removing the duplicate elements.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<int> dataSource1 = new List<int>() { 1, 2, 3, 4, 5, 6 };
List<int> dataSource2 = new List<int>() { 1, 3, 5, 8, 9, 10 };
//Method Syntax
var MS = dataSource1.Union(dataSource2).ToList();
//Query Syntax
var QS = (from num in dataSource1
select num)
.Union(dataSource2).ToList();
foreach (var item in MS)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}

Run the application and you will see the output as expected.

Note: In query syntax, there is no such operator call Union, so here we need to use the mixed syntax i.e. using both query and method syntax.

Example2: 

Here we have two collections of countries and we need to return all the countries from both the collections by removing the duplicate country names.

using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
string[] dataSource1 = { "India", "USA", "UK", "Canada", "Srilanka" };
string[] dataSource2 = { "India", "uk", "Canada", "France", "Japan" };
//Method Syntax
var MS = dataSource1.Union(dataSource2).ToList();
//Query Syntax
var QS = (from country in dataSource1
select country)
.Union(dataSource2).ToList();
foreach (var item in MS)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}

Now run the application and it will give us the following output.

As you can see it displays the country “UK” twice. This is because the default comparer that is being used by the LINQ Union method is case-insensitive.

So if you want to ignore the case-sensitive then you need to use the other overloaded version of the Union() method which takes IEqualityComparer as an argument. So, modify the program as shown below where we pass StringComparer as an argument.

using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
string[] dataSource1 = { "India", "USA", "UK", "Canada", "Srilanka" };
string[] dataSource2 = { "India", "uk", "Canada", "France", "Japan" };
//Method Syntax
var MS = dataSource1.Union(dataSource2, StringComparer.OrdinalIgnoreCase).ToList();
//Query Syntax
var QS = (from country in dataSource1
select country)
.Union(dataSource2, StringComparer.OrdinalIgnoreCase).ToList();
foreach (var item in MS)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
}
}

Now run the application and it should display the data as expected as shown below

 .

C# LINQ Union() Method with Complex Type:

The LINQ Union() Method like other Set Operators such as Distinct, Expect, Intersect is also worked in a different manner when working with complex types such as Product, Employee, Student, etc. Let us understand this with an example.

Create a class file with the name Student.cs and then copy and paste the following code in it.

namespace LINQDemo
{
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
}
}

The above student class is created with just two properties. Let say, we have the following two data sources.

LINQ Union() Method with Complex Type

As you can see in the above image, we have two collections of student data. And if you notice we have two students which are appeared in both the collections.

Example3:

Our requirement is to fetch all the student names from both the collections by removing the duplicate name.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> StudentCollection1 = new List<Student>()
{
new Student {ID = 101, Name = "Preety" },
new Student {ID = 102, Name = "Sambit" },
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
};
List<Student> StudentCollection2 = new List<Student>()
{
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
new Student {ID = 107, Name = "Pranaya"},
new Student {ID = 108, Name = "Santosh"},
};
//Method Syntax
var MS = StudentCollection1.Select(x => x.Name)
.Union(StudentCollection2.Select(y => y.Name)).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std.Name)
.Union(StudentCollection2.Select(y => y.Name)).ToList();
foreach (var name in MS)
{
Console.WriteLine(name);
}
Console.ReadKey();
}
}
}

Output:

Example4:

Now we need to select all the information of all the students from both the collections by removing the duplicate students. In order to do this, let us modify the program class as shown below.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> StudentCollection1 = new List<Student>()
{
new Student {ID = 101, Name = "Preety" },
new Student {ID = 102, Name = "Sambit" },
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
};
List<Student> StudentCollection2 = new List<Student>()
{
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
new Student {ID = 107, Name = "Pranaya"},
new Student {ID = 108, Name = "Santosh"},
};
//Method Syntax
var MS = StudentCollection1.Union(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Union(StudentCollection2).ToList();
foreach (var student in MS)
{
Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
}

Once you run the application, then you will see that it display all the students without removing the duplicate students. This is because the default comparer which is used for comparison is only checked whether two object references are equal and not the individual property values of the complex object. Let us see how to use an anonymous type to solve the above problem.

Using Anonymous Type for Union Operation using Linq Union Method:

In this approach, we need to select all the individual properties to an anonymous type. The following program does exactly the same things.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> StudentCollection1 = new List<Student>()
{
new Student {ID = 101, Name = "Preety" },
new Student {ID = 102, Name = "Sambit" },
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
};
List<Student> StudentCollection2 = new List<Student>()
{
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
new Student {ID = 107, Name = "Pranaya"},
new Student {ID = 108, Name = "Santosh"},
};
//Method Syntax
var MS = StudentCollection1.Select(x => new { x.ID, x.Name })
.Union(StudentCollection2.Select(x => new { x.ID, x.Name })).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select new { std.ID, std.Name })
.Union(StudentCollection2.Select(x => new { x.ID, x.Name })).ToList();
foreach (var student in MS)
{
Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
}

Now run the application and it should display the output as expected as shown below.

Let us see how to achieve the same thing using Comparer.

Using IEqualityComparer :

In this approach, we need to create a class and then we need to implement the IEqualityComparer interface. So, create a class file with the name StudentComparer.cs and then copy and paste the following code in it.

using System.Collections.Generic;
namespace LINQDemo
{
public class StudentComparer : IEqualityComparer<Student>
{
public bool Equals(Student x, Student y)
{
return x.ID == y.ID && x.Name == y.Name;
}
public int GetHashCode(Student obj)
{
return obj.ID.GetHashCode() ^ obj.Name.GetHashCode();
}
}
}

Now, we need to create an instance of StudentComparer class and then we need to pass that instance to the LINQ Union method as shown in the below program.

using System.Collections.Generic;
using System;
using System.Linq;
namespace LINQDemo
{
class Program
{
static void Main(string[] args)
{
List<Student> StudentCollection1 = new List<Student>()
{
new Student {ID = 101, Name = "Preety" },
new Student {ID = 102, Name = "Sambit" },
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
};
List<Student> StudentCollection2 = new List<Student>()
{
new Student {ID = 105, Name = "Hina"},
new Student {ID = 106, Name = "Anurag"},
new Student {ID = 107, Name = "Pranaya"},
new Student {ID = 108, Name = "Santosh"},
};
StudentComparer studentComparer = new StudentComparer();
//Method Syntax
var MS = StudentCollection1
.Union(StudentCollection2, studentComparer).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std)
.Union(StudentCollection2, studentComparer).ToList();
foreach (var student in MS)
{
Console.WriteLine($" ID : {student.ID} Name : {student.Name}");
}
Console.ReadKey();
}
}
}

See All

Comments (466 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 /466

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 /466

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 /466