C# Enumerable Any(IEnumerable, Func)

Description

Enumerable Any (IEnumerable, Func)Determines whether any element of a sequence satisfies a condition.

Syntax


public static bool Any<TSource>(
  this IEnumerable<TSource> source,
  Func<TSource, bool> predicate
)

Parameters

  • TSource - The type of the elements of source.
  • source - An IEnumerable whose elements to apply the predicate to.
  • predicate - A function to test each element for a condition.

Returns

Returns true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.

Example

The following code example demonstrates how to use Any to determine whether any element in a sequence satisfies a condition.


using System;//from  w w  w .  j  a  v a2  s.co m
using System.Linq;
using System.Collections.Generic;

class Employee{
  public string Name { get; set; }
  public int Age { get; set; }
  public bool Trained { get; set; }
}

public class MainClass{
  public static void Main(String[] argv){  
    Employee[] Employees =
        { new Employee { Name="A", Age=38, Trained=true },
          new Employee { Name="B", Age=44, Trained=false },
          new Employee { Name="C", Age=51, Trained=false } };

    bool unTrained = Employees.Any(p => p.Age > 21 && p.Trained == false);

    Console.WriteLine(unTrained);
  }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System.Linq »




Enumerable