Using LINQ Contains to Check Data

Using LINQ Contains to Check Data

linq

23 Articles

Improve

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.

Table of Contents

  1. Introduction
  2. Using LINQ Contains to find Primitive types
  3. Using LINQ Contains to find Object types
  4. Summary

Introduction

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.

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?.

Code Sample - LINQ Contains

Demo - LINQ Contains Clause Demo

Let's try LINQ Contains

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the color to find if any color matches the available colors.
  • Click on Contains Button
  • Click on reset to try other combination

Result:

Color
Black
Red
Black
Red
Brown
White

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.

  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 Contains Product Comparer

Code Sample - LINQ Contains With Product Comparer

Demo - LINQ Contains Clause with Comparer Demo

Let's try LINQ Contains with Comparer

  • We have Product class with following properties - Id, Name, Color, Price, Size
  • Enter the product id to find if any product matches the condition.
  • Click on Contains Button
  • Click on reset to try other combination

Result:

Id Name Color Price Size
1 Shirt Black 1000 58
2 Shirt Red 1000 58
3 Shirt Black 1000 58
4 Shirt Red 1000 58
5 Shirt Brown 1000 58
6 Shirt White 1000 58

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.

  • Linq
  • Contains
  • Check
  • EqualityComparer