Using LINQ Union to combine data

Using LINQ Union to combine data

linq

23 Articles

Improve

In this article, let's learn about how to use Union in LINQ in .NET.

Note: If you have not done so already, I recommend you read the article on Using LINQ Intersect to Find Common data.

Table of Contents

  1. Introduction
  2. Using LINQ Union to combine primitive types
  3. Using LINQ Union to combine with Equality Comparer
  4. Using LINQ Union By to combine objects
  5. Summary

Introduction

When working with two collections, we can combine them using LINQ Union() method. This will combine two collections and gives a single collection with out any duplicates.

LINQ Union() is used to answer questions about collection such as

  • Git merge and combine files without duplicates
  • Combine customers who have participated in specific sale

Using LINQ Union to combine primitive types

Primitive data types like int, decimal, string, etc can just compare the values against other value in the collection

Code Sample - LINQ Union Primitive Types

Demo - LINQ Union Clause Demo

Let's try LINQ Union with integer type

  1. Enter number and add it to respective sequence
  2. For demo purpose I have restricted sequence length to 3
  3. Click on Union Button to view the result
  4. Click on reset to try other combination

Combined without duplicates :

Using LINQ Union to combine with Equality Comparer

So, combining primitive data types with Union() is easy and straight forward, but with objects by default it's going to work by comparing object references. But in most cases we want to make comparison based on one or more properties in the object. To do that we need to start by creating EqualityComparer<T> class.

  1. Create a ProductComparer class that inherits from EqualityComparer<Product> class.
  2. Override Equals(Product 1, Product 2) method.
  3. Write the conditions to check equality and return true if both matches.
  4. Also override GetHashCode() method and return unique value for every single object.

Code Sample - LINQ Union Product Comparer

Code Sample - LINQ Union With Product Comparer

Demo - LINQ Union Clause with Comparer Demo

Let's try LINQ Union with Comparer

  • We have Product class with following properties - Id, Name
  • The tables are loaded with two product collection.
  • Click on Union Button to view the result.
  • Unlike previous demo, here we use ProductComparer to compare objects based on their property values. So they will be equal irrespective of reference.
  • Click on reset to try other combination
Id Name
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt

Combined Products without duplicates :

Id Name

Using LINQ Union By to combine objects

Starting .NET 6, Microsoft added UnionBy() which is similar to Union in functionality but with an simplification that we don't need to use a comparer class. We can use Key Expression to combine items.

Code Sample - LINQ Union By Object Types

Demo - LINQ Union By Clause Demo

Let's try LINQ Union By with object type

  1. We have two Product class with following properties - Id, Name
  2. The tables are loaded with product collection respectively.
  3. The Key Expression here is the Product ID. UnionBy() will compare and combine Products from collection.
  4. For demo purpose I will be displaying only Product ID's
  5. Click on Union By Button to view the result.
  6. Click on reset to try other combination
Id Name
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt

Combined Products without duplicates :

Id Name

Summary

In this article we learn't how to combine data between collections using Union and UnionBy. This can be used to combine items between collection and return a unified collection without duplicates. All these can be used with any IEnumerable or IQueryable types.

  • Linq
  • Union
  • UnionBy
  • Concatenate
  • Combine