Skip to content
This repository was archived by the owner on Jan 14, 2025. It is now read-only.

Files

Latest commit

72ebd64 · Sep 21, 2019

History

History
42 lines (24 loc) · 1.69 KB

elements.md

File metadata and controls

42 lines (24 loc) · 1.69 KB

LINQ - Element Operators

The methods First, FirstOrDefault, and ElementAt provide ways to select a single element by position.

Find the first element

This sample uses First to return the first matching element as a Product, instead of as a sequence containing a Product.

Find the first matching element

This sample uses First to find the first element in the array that starts with 'o'.

First element of a possibly empty sequence

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.

First matching element or default

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.

Find element at position

This sample uses ElementAt to retrieve the second number greater than 5 from an array.

Next: Generate sequences » Previous: Conversion operations »

Home