FirstOrDefault
The LINQ operator that returns the first matching item, or null (or zero for value types) when nothing matches.
In more detail
Its sibling First throws InvalidOperationException when nothing matches. Choose by asking whether a missing item is a bug (First) or an ordinary outcome (FirstOrDefault).
Related terms
- LINQ — Language Integrated Query: a set of C# operators such as Where, Select and OrderBy for filtering, shaping and combining sequences of data.
- SingleOrDefault — The LINQ operator that returns the only matching item or null, and throws if more than one item matches.
- InvalidOperationException — The .NET exception thrown when a call is not valid for the object's current state, such as First on a sequence with no match.
Taught in
- Collections, Generics, and LINQ — Modern .NET Core, C# & Enterprise Microservices