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

Using LINQ Except to Find Difference in data

LINQ

26 Articles

Improve

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

Note: If you have not done so already, I recommend you read the article on Using LINQ Sequence Equal to Find Equality.

Table of Contents

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

Introduction

When working with two collections, we can find the difference between them using LINQ Except() method. The idea here is to find out values in one which is not in other and returns the list of exceptions. With Except(), we can compare two collections for Difference.

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

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

Using LINQ Except 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 Except Primitive Types

Demo - LINQ Except Clause Demo

Let's try LINQ Except 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 Except Button to view the result
  4. Click on reset to try other combination

Difference :

Using LINQ Except to compare objects

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

Code Sample - LINQ Except Objects

Demo - LINQ Except Clause Objects Demo

Let's try LINQ Except with objects

  • We have Product class with following properties - Id, Name
  • We have Sale class with following properties - ProductId, Price
  • The tables are loaded with product and sale collection.
  • Click on Except Button to view the result.
  • Click on reset to try other combination
Products
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Sales
Product Id Price
1 1000
1 1000
2 1000

Products Not Sold :

Using LINQ Except to compare with Equality Comparer

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

Code Sample - LINQ Except With Product Comparer

Demo - LINQ Except Clause with Comparer Demo

Let's try LINQ Except with Comparer

  • We have Product class with following properties - Id, Name
  • The tables are loaded with two product collection.
  • Click on Except 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
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt

Difference Product Id :

Using LINQ Except By to compare objects with Primitive Types

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

Code Sample - LINQ Except By Primitive Types

Demo - LINQ Except By Clause Demo

Let's try LINQ Except By with integer type

  1. Enter number and add it to sequence
  2. For demo purpose I have restricted sequence length to 3
  3. The Key Expression here is the Product ID. So whatever ID you enter in above sequence, ExceptBy() will compare and return the remaining Products from collection.
  4. For demo purpose I will be displaying only Product ID's
  5. Click on Except By Button to view the result
  6. Click on reset to try other combination
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt

Difference Product Ids :

Using LINQ Except By to compare objects

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

Code Sample - LINQ Except By Objects

Demo - LINQ Except By Clause Demo

Let's try LINQ Except By with objects

  • We have Product class with following properties - Id, Name
  • We have Sale class with following properties - ProductId, Price
  • The tables are loaded with product and sale collection.
  • The Key Expression here is the Product ID. So whatever ID you enter in above sequence, ExceptBy() will compare and return the remaining Products from collection.
  • For demo purpose I will be displaying only Product ID's
  • Click on Except By Button to view the result.
  • Click on reset to try other combination
Products
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Sales
Product Id Price
1 1000
1 1000
2 1000

Products Not Sold :

Summary

In this article we learn't how to check for difference in data between collections using Except and ExceptBy. 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
  • Except
  • ExceptBy
  • Difference
  • Compare