LINQ Interview Question #40
What does First do in LINQ?
Filtering, Projection, Ordering & Element Operators Junior Beginner
Quick Interview Answer
First returns the first element of a sequence. An overload can return the first element satisfying a predicate.
Detailed Explanation
First returns the first element of a sequence. An overload can return the first element satisfying a predicate.
If no matching element exists, First throws InvalidOperationException. Use it when the application expects an element to exist and absence represents an exceptional or invalid state.
Code Example
var firstProduct = products.First();
var firstActive =
products.First(p => p.IsActive);