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.