LINQ Interview Question #71

What does Any do in LINQ?

Joins, Grouping, Set & Quantifier Operators Mid-Level Intermediate

Quick Interview Answer

Any determines whether a sequence contains at least one element. With a predicate, it determines whether at least one element satisfies the condition.

Detailed Explanation

Any determines whether a sequence contains at least one element. With a predicate, it determines whether at least one element satisfies the condition.

It can short-circuit as soon as a match is found, so it is usually clearer than counting all matching elements merely to test existence.

Code Example

bool hasProducts = products.Any();

bool hasOutOfStock = products
    .Any(p => p.Stock == 0);