Part - 14 LINQ Union Method
Part - 14 LINQ Union Method
Part - 14 LINQ Union Method
The LINQ Union Method in C# is used to combine multiple data sources into one data source by
removing duplicate elements. There are two overloaded versions available for the LINQ Union Method
as shown below.
The one and only difference between the above two LINQ Union methods is that the second
overloaded version takes IEqualityComparer as an argument. That means when we are working with
Complex Data Types, in order to work as expected, we can use the overloaded method which takes
the IEqualityComparer parameter.
//Method Syntax
var MS = dataSource1.Union(dataSource2).ToList();
//Query Syntax
var QS = (from num in dataSource1
select num)
.Union(dataSource2).ToList();
Console.ReadKey();
}
}
}
Run the application and you will see the output as expected i.e. 1 2 3 4 5 6 8 9 10
//Method Syntax
var MS = dataSource1.Union(dataSource2).ToList();
//Query Syntax
var QS = (from country in dataSource1
select country)
.Union(dataSource2).ToList();
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. Let us modify the Main method of the program class as
follows to use the StringComparer as an argument that implements
the IEqualityComparer interface.
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();
Console.ReadKey();
}
}
}
//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();
Console.ReadKey();
}
}
}
When you run the above code, you will get the output as expected as shown in the below image.
Here, you can see, the names Hina and Anurag appeared only once.
Now our requirement has changed and now we need to select all the information of all the students
from both collections by removing the duplicate students. In order to do this, let us modify the Main
Method of 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"},
};
//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 displays all the students without removing the
duplicate students as shown in the below image.
As you can see in the above output, the students Hina and Anurag appeared twice. This is because
the default comparer which is used for comparison by the LINQ Union Method is only checked
whether two object references are equal and not the individual property values of the complex object.
As we already discussed in our previous three articles, we can overcome this problem in many
different ways. Let us understand each way one by one.
//Method Syntax
var MS = StudentCollection1
.Union(StudentCollection2, studentComparer).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std)
.Union(StudentCollection2, studentComparer).ToList();
Console.ReadKey();
}
}
}
With the above changes in place, run the application and you will get the output as expected as
shown in the below image. Here, you can see Hina and Anurag appeared only once.
//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();
Console.ReadKey();
}
}
}
With the above changes in place, now run the application and you will get the output as expected as
shown in the below image.
//Method Syntax
var MS = StudentCollection1.Union(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Union(StudentCollection2).ToList();
Console.ReadKey();
}
}
}
Now run the application code and, you will get the same output as the previous two examples as
expected as shown in the below image.
//Method Syntax
var MS = StudentCollection1.Union(StudentCollection2).ToList();
//Query Syntax
var QS = (from std in StudentCollection1
select std).Union(StudentCollection2).ToList();
Console.ReadKey();
}
}
}
Now, run the application and you will also get the same output as expected as shown in the below
image.