Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

LINQ / C#

LINQ Contains Operators

LINQ Contains operator is used to check whether an element is available in sequence (collection) or not. Contains operator comes under Quantifier Operators category in LINQ Query Operators.

Below is the syntax of Contains operator.

public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value);
public static bool Contains<TSource>(this IEnumerable<TSource> source, TSource value, IEqualityComparer<TSource> comparer);

First overload takes a single parameter. Second overload function is used when we have to compare complex types or we have to do custom comparison. By default, Contains operator match reference of complex type. We used second overload to match any properties of complex types.

C# LINQ Contains Example

List<string> names = new List<string>();
names.Add("Kapil");
names.Add("Ramesh");
names.Add("Raj");
names.Add("Anil");

bool result =  names.Contains("Raj");
Console.WriteLine(result);

Result
------
True

In the above example, we take a List of strings. In the 7th line, we use Contains operator to check whether any student name “Raj” is available in the collection or not. 

C# LINQ Contains with IEqualityComparer

internal class Student
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Rank { get; set; }
    public int Age { get; set; }
}

internal class StudentNameComparer : IEqualityComparer<Student>
{
    public bool Equals(Student x, Student y)
    {
        if (string.Equals(x.Name, y.Name, StringComparison.OrdinalIgnoreCase))
        {
            return true;
        }
        return false;
    }

    public int GetHashCode(Student obj)
    {
        return obj.Name.GetHashCode();
    }
}
public class Program
{
    public static void Main(string[] args)
    {
        List<Student> students = new List<Student>();
        students.Add(new Student { Id = 1, Name = "Kapil", Age = 22 });
        students.Add(new Student { Id = 2, Name = "Ramesh", Age = 17 });
        students.Add(new Student { Id = 3, Name = "Raj", Age = 24 });
        students.Add(new Student { Id = 4, Name = "Anil", Age = 25 });

        var studentToFind = new Student { Name = "Raj" };
        var result = students.Contains(studentToFind, new StudentNameComparer());
        Console.WriteLine(result);
    }
}

Result
------
True

In the above example, we have created a complex type Student. We added a number of Student types in students sequence. To check whether a particular student exists in this collection or not, we created a class StudentNameComparer which implements the IEqualityComparer<Student> interface.

IEqualityComparer interface contains two methods Equals and GetHashCode. Both methods are must to implement. In the Equals method, we match student name property and in GetHashCode method we return hash code of name property.

In line 35, we created a new student class and fill only name property and pass StudentNameComparer to second parameter of Contains method. Contains operator used the Equals and GetHashCode of StudentNameComparer class to compare only Name property to find that particular student.