c sharp

C# LINQ Union() Method

Language Integrated Query language (LINQ) is used to perform operations on the C# collections or Normal data structures. It is used to perform queries similar to SQL Like expressions.

In this article, we will discuss how to perform Union operation using Union() method through LINQ.

LINQ Union()

Union() in LINQ is used to return all elements uniquely from two given data structures. The elements can be numeric type or string type.

Syntax:

Data1.Union(Data2)

Data1 is the first data structure and Data2 is the second data structure.

Example 1:

Let’s create two list data structures with Integer elements and return all the values uniquely using the Union() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create first data
       List<int> first_list = new List<int>() { 345,678,456,890 };
       
       //create second data
       List<int> second_list = new List<int>() { 345,890,324,564,433};
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Union operation on two Lists and store the result into list.
       var operation = first_list.Union(second_list).ToList();
           
         Console.WriteLine("Union operation on two Integer Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output:

Explanation:

Lists Creation:

We have to specify the data type inside List.

Union Operation:

After performing Union operation, we will store the result in List.

Result:

We are displaying one by one element using the foreach() loop.

Finally, the elements from both the Lists uniquely are : 345,678,456,890,324,564, and 433.

Example 2:

Let’s create two list data structures with double elements and return all the values uniquely using the Union() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
        //create first data
       List<double> first_list = new List<double>() { 1.20,4.5,7.0,8.99 };
       
       //create second data
       List<double> second_list = new List<double>() { 1.20,4.5,7.0,8.99};
       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Union operation on two Lists and store the result into list.
       var operation = first_list.Union(second_list).ToList();
           
         Console.WriteLine("Union operation on two Integer Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output:

Explanation:

Lists Creation:

We have to specify the data type double inside List and pass the elements.

Union Operation:

After performing Union operation, we will store the result into List.

Result:

We are displaying one by one element using the foreach() loop.

Finally, the elements from both the Lists uniquely are: 1.2,4.5,7, and 8.99.

Example 3:

Let’s create two list data structures with string elements and return the values uniquely using the Union() method.

using System;
using System.Linq;
using System.Collections.Generic;

 //create a class - Linuxhint
class Linuxhint
{
 
    static public  void Main(){
       
         //create first data
       List<string> first_list = new List<string>() {"welcome","to","Linuxhint" };
       
       //create second data
       List<string> second_list = new List<string>() { "Linuxhint"};

       
       Console.WriteLine("First List: ");
           foreach (var values in first_list)
            {
                Console.WriteLine(values);
            }
            Console.WriteLine("Second List: ");
           foreach (var values in second_list)
            {
                Console.WriteLine(values);
            }
           
        //perform Union operation on two Lists and store the result into a list.
       var operation = first_list.Union(second_list).ToList();
           
         Console.WriteLine("Union operation on two Integer Lists: ");
         
         //return one by one value from the operation
        foreach (var result in operation)
            {
                Console.WriteLine(result);
            }
           
}
}

Output:

The elements from both the Lists uniquely are: welcome, to, Linuxhint.

Conclusion

We learned the two data sources that are converted to Array in C# through LINQ. We learned how to convert the List, SortedSet, and HashSet to List using the ToArray() method with three examples by considering the Inter and String type values.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain