Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

Part 31 - Quantifiers in LINQ

Suggested Videos
Part 28 - Generation Operators in LINQ
Part 29 - Concat operator in LINQ
Part 30 - SequenceEqual Operator in LINQ



The following methods belong to Quantifiers category
All
Any
Contains



All these methods return true or false depending on whether if some or all of the elements in a sequence satisfy a condition.

All() method returns true if all the elements in a sequence satisfy a given condition, otherwise false.

Example 1 : Returns true, as all the numbers are less than 10

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.All(x => x < 10);

Console.WriteLine(result);

There are 2 overloaded versions of Any() method. The version without any parameters checks if the sequence contains at least one element. The other version with a predicate parameter checks if the sequence contains at least one element that satisfies a given condition.

Example 2 : Returns true as the sequence contains at least one element

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Any();

Console.WriteLine(result);

Example 3 : Returns false as the sequence does not contain any element that satisfies the given condition (No element in the sequence is greater than 10)

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Any(x => x > 10);

Console.WriteLine(result);

There are 2 overloaded versions of the Contains() method. One of the overloaded version checks if the sequence contains a specified element using the default equality comparer. The other overloaded version checks if the sequence contains a specified element using an alternate equality comparer.

Example 4 : Returns true as the sequence contains number 3. In this case the default equality comparer is used.

int[] numbers = { 1, 2, 3, 4, 5 };

var result = numbers.Contains(3);

Console.WriteLine(result);

Example 5 : Returns true. In this case we are using an alternate equality comparer (StringComparer) for the comparison to be case-insensitive.

string[] countries = { "USA", "INDIA", "UK" };

var result = countries.Contains("india", StringComparer.OrdinalIgnoreCase);

Console.WriteLine(result);

When comparing complex types like Employee, Customer etc, the default comparer will only check if the object references are equal, and not the individual property values of the objects that are being compared.

Example 6 : Returns false, as the default comparer will only check if the object references are equal.

List<Employee> employees = new List<Employee>()
{
    new Employee { ID = 101, Name = "Rosy"},
    new Employee { ID = 102, Name = "Susy"}
};

var result = employees.Contains(new Employee { ID = 101, Name = "Rosy" });

Console.WriteLine(result);

To solve the problem in Example 6, there are 3 ways
1. Use the other overloaded version of Contains() method to which we can pass a custom class that implements IEqualityComparer
2. Override Equals() and GetHashCode() methods in Employee class
3. Project the properties into a new anonymous type, which overrides Equals() and GetHashCode() methods

We discussed implementing these 3 options for Distinct() method in Part 26 of LINQ Tutorial. In the same way these options can be implemented for Contains() method.

linq tutorial

7 comments:

  1. please upload video on linq to dataset please

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Hello! I'm trying to use Contains method, i.e. to solve the problem of comparing complex types (such as Employee) by third method ("Project the properties into a new anonymous type"). But when I project to a new anonymous type, compiler doesn't accept old type (Employee) into Contains method. How should I solve the problem?

    var NewEmp = new Employee { ID = 1, Name = "Max"};
    var result = empList.Select(e => new
    {
    ID = e.ID,
    Name = e.Name
    }).Contains(NewEmp);//compiler error on last line.

    Thank you!

    ReplyDelete
    Replies
    1. var result = empList.Select(e => new
      {
      ID = e.ID,
      Name = e.Name
      }).Any(emp => (emp.ID == 1 && emp.Name == "Max"));

      Delete
    2. var result = employees.Select(x => new { x.ID, x.Name }).Contains(new { ID = 101, Name = "Max" });

      Delete
    3. thankyou Ashish Kumar it is working, we should include only new without naming employee, because they we are using anonymous.

      Delete
  4. Shall you Mr to upload video in LINQ To Database

    ReplyDelete

It would be great if you can help share these free resources