
Using LINQ Contains to Check 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 Contains in LINQ in .NET.
Note: If you have not done so already, I recommend you read the article on Using LINQ Any to Find Type of Data.
We can determine the type of data contained within the collection using LINQ Contains() method. The idea here is to find out if any items in a collection meet a specific condition. For primitive data types like int, decimal, string, etc we can just compare the values against other value in the collection but for Class, we need to implement EqualityComparer class to do comparisons.
Why we gonna do?
Checking whether a specific value exists in a collection without writing a manual loop is a frequent need. Contains() handles primitive types with a simple equality check and supports complex objects through a custom EqualityComparer, enabling property-based comparison instead of reference equality.
How we gonna do?
Using LINQ Contains to find Primitive types
LINQ Contains() is used to answer questions about collection such as, any students passed the exam?, Do any orders got shipped? Do any customers opted for newsletters?. Let's take a look at syntax. The syntax for Contains() is we apply the Contains() method to some IEnumerable<T> collection and we specify a predicate. This is then going to check if any items within the collection match the given condition. For example, IEnumerable<T>.Contains(predicate). This will return a boolean value true or false indicating do any element in the collection met the criteria?.
List<Product> products = GetProducts();
//Method Syntax
bool result = products
.Select(product => product.Color)
.Contains("Red");
//Query Syntax
bool result = (from product in products select product)
.Select(product => product.Color)
.Contains("Red");
Using LINQ Contains to find Object types
So, searching for primitive data types with Contains() 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 ProductIdComparer : EqualityComparer<Product>
{
public override bool Equals(Product? product, Product? anotherProduct)
{
return product?.Id == anotherProduct?.Id;
}
public override int GetHashCode([DisallowNull] Product product)
{
return product.Id.GetHashCode();
}
}
List<Product> products = GetProducts();
Product product = products.Last();
ProductIdComparer productIdComparer = new();
//Method Syntax
bool result = products
.Contains(product, productIdComparer);
//Query Syntax
bool result = (from product in products select product)
.Contains(product, productIdComparer);
Summary
In this article we learn't how to check for data within collection using Contains. This can be used to find if item is within collection matches a criteria or not and also we can find object item using Comparer. All these can be used with any IEnumerable or IQueryable types.