Javatpoint Logo
Javatpoint Logo

LINQ FirstOrDefault() Method

In LINQ, FirstOrDefault() Operator is used to return the first element from the list/collection. FirstOrDefault() Operator is same as LINQ First() Operator and the only difference is, in case if the lists returns no elements then LINQ FirstOrDefault operator method will return default value.

Syntax of FirstOrDefault Method

Here is the syntax of the LINQ FirstOrDefault operator to return the first element from the list or in case if lists return no element.

From the above syntax, we are trying to get the First element or default element from the "objList" collection by using LINQ FirstOrDefault() operator.

Example of LINQ FirstOrDefault() operator

Here is the example of using the LINQ FirstOrDefault() operator in method syntax to return the first element from the list or in case if the list does not contain any value.

In the above example, we have two lists "ListObj", "objVal" and we are trying to get the first element from the lists by using the LINQ FirstOrDefault() method.

Output:

LINQ FirstOrDefault() Method

Example of LINQ FirstOrDefault () operator in Query Syntax

Here is the example of using the LINQ FirstOrDefault() operator in query syntax to return the first element from the list or in case if the list does not contain any value.

Output:

LINQ FirstOrDefault() Method




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA