LINQ Union in C#

LINQ (Language-Integrated Query) is a powerful technology that enables developers to query and manipulate data in a more intuitive and efficient way. One of the set operators provided by LINQ is Union. The Union operator allows us to combine two sequences and retrieve only the distinct elements from them. In this lesson, we will discuss the syntax of LINQ Union, its usage, and some examples to better understand its functionality.

Syntax:

The syntax for LINQ Union is as follows:

var result = sequence1.Union(sequence2);

Here, sequence1 and sequence2 are two sequences that we want to combine. The Union() method takes two parameters, which are the two sequences we want to combine, and returns a new sequence with distinct elements from both sequences.

Usage:

The Union operator is useful in scenarios where we have to combine two sequences and retrieve only the distinct elements. For example, suppose we have two lists, one containing names of employees and the other containing names of contractors. We need to create a list of all the people working in the company, without any duplicates. In this scenario, we can use the Union operator to combine the two lists and get a new list with distinct elements.

Examples:

Let’s take a look at some examples to better understand how to use LINQ Union in C#.

Example 1: Combining two integer sequences

int[] sequence1 = { 1, 2, 3, 4, 5 };
int[] sequence2 = { 4, 5, 6, 7, 8 };
var result = sequence1.Union(sequence2);
foreach (int number in result)
{
    Console.WriteLine(number);
}

In this example, we have two integer sequences, sequence1 and sequence2. We use the Union operator to combine the two sequences and store the result in a new sequence called result. The foreach loop iterates through the result sequence and prints the distinct elements.

Example 2: Combining two string sequences

string[] sequence1 = { "John", "Michael", "Samuel", "David" };
string[] sequence2 = { "Samuel", "Mary", "Jennifer", "David" };
var result = sequence1.Union(sequence2, StringComparer.OrdinalIgnoreCase);
foreach (string name in result)
{
    Console.WriteLine(name);
}

In this example, we have two string sequences, sequence1 and sequence2. We use the Union operator to combine the two sequences and store the result in a new sequence called result. The StringComparer.OrdinalIgnoreCase option is used to perform a case-insensitive comparison of string elements. The foreach loop iterates through the result sequence and prints the distinct elements.

FAQs:

Q: What is the difference between Union and Concat in LINQ?

A: The Union operator retrieves only the distinct elements from two sequences, whereas the Concat operator retrieves all the elements from two sequences, including duplicates.

Q: Can we use Union with non-primitive types?

A: Yes, we can use Union with non-primitive types as long as the type implements the IEqualityComparer interface.

Conclusion:

The Union operator is a useful tool provided by LINQ to combine two sequences and retrieve only the distinct elements. It is an efficient way to eliminate duplicates from a sequence and create a new sequence with only unique elements. By understanding the syntax and usage of the Union operator, developers can use it to write more efficient and effective code.