LINQ Interview Question #26

What is filtering in LINQ?

Filtering, Projection, Ordering & Element Operators Junior Beginner

Quick Interview Answer

Filtering selects only the elements that satisfy a condition. The primary LINQ filtering operator is Where.

Detailed Explanation

Filtering selects only the elements that satisfy a condition. The primary LINQ filtering operator is Where.

Where accepts a predicate and returns a sequence containing matching elements. It normally uses deferred execution, so the filtering work happens when the sequence is enumerated.

Code Example

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

var expensiveProducts = products
    .Where(p => p.Price >= 1000);