The methods First
, FirstOrDefault
, and ElementAt
provide ways to select a single element by position.
This sample uses First
to return the first matching element as a Product
, instead of as a sequence containing a Product
.
This sample uses First
to find the first element in the array that starts with 'o'.
This sample uses FirstOrDefault
to try to return the first element of the sequence, unless there are no elements, in which case the default value for that type is returned.
This sample uses FirstOrDefault
to return the first product whose ProductID
is 789
as a single Product
object, unless there is no match, in which case null
is returned.
This sample uses ElementAt
to retrieve the second number greater than 5 from an array.
Next: Generate sequences » Previous: Conversion operations »