Javatpoint Logo
Javatpoint Logo

LINQ LastOrDefault() Method

In LINQ, LastOrDfeault() method/operator is used to return the last element from the list/collection, and it is the same as LINQ Last() method, and the only difference is that it will return the default value when there is no element in the list/collection.

Syntax of LINQ LastOrDefault() Method

Here is the syntax of using the LINQ LastOrDefault() method to get the last element from the list or default value. In case, if lists returns no elements using the LINQ LastOrDefault() method.

From the above syntax, we are trying to get the last element from the "LastObj" list using LINQ LastOrDefault() method.

Example of LINQ LastOrDefault() Operator in Method Syntax

Here is the example of using the LINQ LastOrDefault() operator in method syntax to get the last element from the list.

From the above code, we are getting the last element from "LISTOBJ" and "ValObj" lists by using the LINQ LastOrDefault() method.

OUTPUT:

LINQ LastOrDefault() Method

Example of LINQ LastOrDefault()operator in Query Syntax

Here is the example of using the LINQ LastOrDefault() operator in query syntax to get the last element from the list.

OUTPUT:

LINQ LastOrDefault() Method




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA