LINQ Interview Question #15

What is a predicate in LINQ?

LINQ Fundamentals, Syntax, IEnumerable & IQueryable Junior Beginner

Quick Interview Answer

A predicate is a function that evaluates an element and returns true or false. LINQ operators such as Where, Any, All, First, and Count can use predicates to decide which elements satisfy a condition.

Detailed Explanation

A predicate is a function that evaluates an element and returns true or false. LINQ operators such as Where, Any, All, First, and Count can use predicates to decide which elements satisfy a condition.

Predicates can be written inline as lambda expressions or stored in reusable delegates or expressions when appropriate.

Code Example

var activeProducts = products
    .Where(p => p.IsActive);

bool hasExpensiveProduct =
    products.Any(p => p.Price > 1000);