LINQ Any

Summary: in this tutorial, you’ll learn how to use the LINQ Any() method to determine if a sequence contains any elements or if any element in a sequence satisfy a specified condition.

Introduction to the LINQ Any() method

The Any() is an extension method in LINQ that returns true if a sequence contains any element or if the sequence contains any element that satisfies a specified condition. Otherwise, it returns false.

bool Any<TSource> (
   this IEnumerable<TSource> source, 
   Func<TSource,bool> predicate
);Code language: C# (cs)

In this syntax:

  • source is an input sequence with the type IEnumerable<T>.
  • predicate is a function that returns true if each element satisfies a condition.

LINQ Any() examples

Let’s take some examples of using the Any() method.

1) Using LINQ Any() method to check if a sequence has any element

The following example demonstrates how to use the LINQ Any() method to check if a sequence has any element:

using static System.Console;

int[] numbers = { 5, 7, 2, 1, 0 };

bool hasNumber = numbers.Any();

if (hasNumber)
{
    WriteLine("The numbers has element(s).");
}Code language: C# (cs)

Output:

The numbers has an element.Code language: C# (cs)

How it works.

First, define an array of five numbers:

int[] numbers = { 5, 7, 2, 1, 0 };Code language: C# (cs)

Second, use the Any() method to check if the numbers array contains any elements:

bool hasNumber = numbers.Any();Code language: C# (cs)

Third, check the return value of the Any() method and write a message to the console if it is true:

if (hasNumber)
{
    WriteLine("The numbers has element(s).");
}Code language: C# (cs)

2) Using LINQ Any() method to check if any element of a sequence satisfy a condition

The following example demonstrates how to use the LINQ Any() method to check if a sequence has at least one element that satisfies a condition:

using static System.Console;

int[] numbers = { 5, 7, 2, 8, 0 };

bool hasNumberGreaterThanFive = numbers.Any(n => n > 5);

if (hasNumberGreaterThanFive)
{
    WriteLine("The sequence contains at least a number greater than 5");
}Code language: C# (cs)

In this example, the lambda expression n => n > 5 tests each number in the numbers array. If an element is greater than 5, the Any() method returns true and stops evaluating the remaining elements of the array.

3) Using LINQ Any() method with a sequence of objects

The following program demonstrates how to use the Any() method to check if there is any employee with a salary higher than 60K:

using static System.Console;

namespace LINQDemo
{
    class Employee
    {
        public string? Name { get; set; }
        public string? Department { get; set; }
        public int Salary { get; set; }

    }
    class Program
    {
        public static void Main()
        {
            var employees = new List<Employee>() {
                new() { Name = "John", Department = "HR", Salary = 50000 },
                new() { Name = "Jane", Department = "IT", Salary = 60000 },
                new() { Name = "Bob",  Department = "HR", Salary = 45000 },
                new() { Name = "Sara", Department = "IT", Salary = 55000 },
                new() { Name = "Tom",  Department = "IT", Salary = 65000 }
            };

            var salaryGt60K = employees.Any(e => e.Salary > 60000);
            if (salaryGt60K)
            {
                WriteLine("At least one employee with salary higher than 60K");
            }

        }
    }
}Code language: C# (cs)

Output:

At least one employee with salary higher than 60KCode language: JavaScript (javascript)

In this example, the lambda expression e => e.Salary > 60000 applies to each Employee object and returns true if the value of the Salary property is greater than 60000.

Summary

  • Use LINQ Any() method to determine if a sequence contains any elements or if any element in a sequence satisfy a specified condition.
Was this tutorial helpful ?