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#.
- What is LINQ Union in C#?
- Examples of C# LINQ Union Method using both Method and Query Syntax
- Using C# Linq Union Method with Complex Type.
- How to implement IEqualityComparer?
- 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.
Let us understand this with an example. Please have a look at the following image.
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.
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.
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.
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.
The above student class is created with just two properties. Let say, we have the following two data sources.
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.
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.
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.
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.
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.