
Using LINQ Sequence Equal to Find Equality of data
Author - Abdul Rahman (Bhai)
LINQ
26 Articles
Table of Contents
What we gonna do?
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.
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.
Why we gonna do?
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
How we gonna do?
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
List<int> list = new() { 1, 2, 3, 4, 5 };
List<int> anotherList = new() { 1, 2, 3, 4, 5 };
//Method Syntax
bool result = list
.SequenceEqual(anotherList);
//Query Syntax
bool result = (from number in list select number)
.SequenceEqual(anotherList);
Using LINQ Sequence Equal to compare objects
Sequence Equal checks for reference equality of two objects for object data types.
List<Product> products = GetProducts();
List<Product> anotherProducts = GetProducts();
//Point to same reference to make SequenceEqual True
//products = anotherProducts;
//Method Syntax
bool result = products
.SequenceEqual(anotherProducts);
//Query Syntax
bool result = (from product in products select product)
.SequenceEqual(anotherProducts);
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.
- Create a ProductComparer class that inherits from EqualityComparer<Product> class.
- Override Equals(Product 1, Product 2) method.
- Write the conditions to check equality and return true if both matches.
- Also override GetHashCode() method and return unique value for every single object.
public class ProductComparer : EqualityComparer<Product>
{
public override bool Equals(Product? product, Product? anotherProduct)
{
return (product?.Id == anotherProduct?.Id &&
product?.Name == anotherProduct?.Name &&
product?.Price == anotherProduct?.Price &&
product?.Color == anotherProduct?.Color &&
product?.Size == anotherProduct?.Size);
}
public override int GetHashCode([DisallowNull] Product product)
{
return $"{product.Id}{product.Name}{product.Price}{product.Color}{product.Size}".GetHashCode();
}
}
List<Product> products = GetProducts();
List<Product> anotherProducts = GetProducts();
ProductComparer productComparer = new();
//Method Syntax
bool result = products
.SequenceEqual(anotherProducts, productComparer);
//Query Syntax
bool result = (from product in products select product)
.SequenceEqual(anotherProducts, productComparer);
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.