👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
Using LINQ Intersect to Find Common data

Using LINQ Intersect to Find Common data

LINQ

26 Articles

Improve

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

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

Table of Contents

  1. Introduction
  2. Using LINQ Intersect to compare primitive types
  3. Using LINQ Intersect to compare objects
  4. Using LINQ Intersect to compare with Equality Comparer
  5. Using LINQ Intersect By to compare objects with primitive types
  6. Using LINQ Intersect By to compare objects
  7. Summary

Introduction

When working with two collections, we can find the same data between them using LINQ Intersect() method. The idea here is to find out values in one which is in other and returns the list of common ones. With Intersect(), we can compare two collections for Common items. Intersect() and IntersectBy() are exact opposite of Except() and ExceptBy().

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

  • Find phones that have sales
  • Find customers who have ordered specific product
  • Compare data in tables between different environments

Using LINQ Intersect to compare primitive types

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

Code Sample - LINQ Intersect Primitive Types

Demo Space

Using LINQ Intersect to compare objects

Intersect checks for reference equality of two objects for object data types.

Code Sample - LINQ Intersect Objects

Demo Space

Using LINQ Intersect to compare with Equality Comparer

So, equality for primitive data types with Intersect() 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 Intersect Product Comparer

Code Sample - LINQ Intersect With Product Comparer

Demo Space

Using LINQ Intersect By to compare objects with Primitive Types

Starting .NET 6, Microsoft added IntersectBy() which is similar to Intersect in functionality but with an simplification that we don't need to use a comparer class. We can use Key Expression to find the common.

Code Sample - LINQ Intersect By Primitive Types

Demo Space

Using LINQ Intersect By to compare objects

Previously, we saw a demo on how to find products that are sold. We can also perform the same function using IntersectBy(). Unlike Intersect(), IntersectBy() will return list of Product instead of product Id's.

Code Sample - LINQ Intersect By Objects

Demo Space

Summary

In this article we learn't how to check for common data between collections using Intersect and IntersectBy. This can be used to find if item is between collection matches or not and also we can compare object item using Comparer. All these can be used with any IEnumerable or IQueryable types.

👉🏼 Click here to Join I ❤️ .NET WhatsApp Channel to get 🔔 notified about new articles and other updates.
  • LINQ
  • Intersect
  • IntersectBy
  • Common
  • Same