Using LINQ Sequence Equal to Find Equality of data

Using LINQ Sequence Equal to Find Equality of data

linq

23 Articles

Improve

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

Note: If you have not done so already, I recommend you read the article on Using LINQ Contains to Check Data.

Table of Contents

  1. Introduction
  2. Using LINQ Sequence Equal to compare primitive types
  3. Using LINQ Sequence Equal to compare objects
  4. Using LINQ Sequence Equal to compare with Equality Comparer
  5. Summary

Introduction

When working with two collections, we can find the equality between them using LINQ SequenceEqual() method. The idea here is to find out if two collections are equal or find values in one which is not in other or to find values in common. With SequenceEqual(), we can compare two collections for Equality.

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

  • Read lines from two files and compare like git difference
  • Read data from two different table to check for equality
  • Compare data in tables between different environments

Using LINQ Sequence Equal 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 SequenceEqual Primitive Types

Demo - LINQ SequenceEqual Clause Demo

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

Result :

Using LINQ Sequence Equal to compare objects

Sequence Equal checks for reference equality of two objects for object data types.

Code Sample - LINQ Sequence Equal Objects

Demo - LINQ SequenceEqual Clause Objects Demo

Let's try LINQ SequenceEqual with objects

  • We have Product class with following properties - Id, Name
  • The tables are loaded with two product collection.
  • Click on Sequence Equal Button to view the result.
  • Click on Assign to same reference button to make two collection to point to same reference.
  • Click on reset to try other combination
Products
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt
Another Products
Id Name
1 Shirt
2 Shirt
3 Shirt
4 Shirt
5 Shirt
6 Shirt

Result :

Using LINQ Sequence Equal to compare with Equality Comparer

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

Code Sample - LINQ Sequence Equal With Product Comparer

Demo - LINQ SequenceEqual Clause with Comparer Demo

Let's try LINQ SequenceEqual with Comparer

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

Result :

Summary

In this article we learn't how to check for equality in data between collections using SequenceEqual. 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.

  • Linq
  • SequenceEqual
  • Equality
  • Compare