Hello All,
In this article we will leran about First() and FirstOrDefault() method in LINQ.
LINQ stands for Language-Integrated Query. It is a set of technologies that enable you to query and manipulate data from various sources using C# syntax. With LINQ, you can write queries that are checked at compile time, supported by IntelliSense, and integrated with object-oriented programming.
LINQ queries can be written in two ways: query syntax and method syntax. Query syntax is similar to SQL and uses keywords such as from, where, select, and group. Method syntax uses extension methods and lambda expressions to perform the same operations.
Lets discuss about First() and FirstOrDefault()
First()
- Always it Returns first element of a sequence.
- It throw an error when There is no element in the result or source is null.
- you should use it,If more than one element is expected and you want only first element.
- Returns first element of a sequence, or a default value if no element is found.
- It throws an error Only if the source is null.
- you should use it, If more than one element is expected and you want only first element. Also good if result is empty.