LINQ Last Method in C#: 

The LINQ Last Method in C# is used to return the last element from a data source or from a collection. If the data source or collection is empty, or if we specified a condition and with that condition, no matching element is found in the data source, then the LINQ Last method will throw an InvalidOperationException. If the Data Source is Null, then it will throw ArgumentNullException. There are two overloaded versions available for the LINQ Last method as shown in the below image.

As you can see, the first overloaded version of the Last method does not take any parameter. It simply returns the last element from the data source and if the data source is empty then it will throw InvalidOperationException.

The second overloaded version of the Last method takes a predicate as a parameter. Using this predicate we can specify a condition and then it returns the last matching element which satisfied the specified condition. In this case, if no element satisfied the specified condition, then it will throw InvalidOperationException.

Example to Understand LINQ Last Method in C#:

Let us see an example to Understand LINQ Last Method in C# with both Method Syntax and Query Syntax.  For a better understanding, please have a look at the following example. Here we have created one data source i.e. collection which contains integer numbers from 1 to 10. Our requirement is to fetch the first element from the data source i.e. in the output we want to display the value 10 as it is the last element in the data source. And here, we fetch the last element from the collection using the LINQ Last Method. There is no such operator called last available in LINQ to write the Query Syntax, If you want then you can combine both the method syntax and query syntax to write the code which is also shown in the below example.

 

Output: 10

 

 
Example to Understand LINQ Last Method which takes Predicate as a Parameter in C#

Now, we need to fetch the last element from the data source which is divisible by 3. Here we need to use the second overloaded version of the Last method using which we can specify our condition. For a better understanding, please have a look at the following example. The following program uses the second overloaded version of the Last Method to return the last element from the data source which is divisible by 3. See, there are many elements that satisfy the given condition, but the last element which satisfies the given condition is going to be returned by the Last method. In this case, the element will be 9. In the below example, I am showing how to use the second overloaded version of the LINQ Last method in C# using both Method and Query Syntax.

Output: 9

 

What Happens when we call the Last Method on Empty Data Source?

Whenever the data source is empty, then the LINQ Last Method will throw InvalidOperationException. For a better understanding, please have a look at the following example. In the below example, we are calling the Last method on an Empty Data Source and hence when we run the following code, we will get one Runtime Exception.

 
What Happens If the Specified Condition in the Last Method Does not Match with Any Element?

If the specified condition does not match with any element in the data source, then the LINQ Last Method will throw InvalidOperationException. For a better understanding, please have a look at the following example. In the below example, we are calling the Last method and specifying the condition to fetch the last element which is greater than 50. As you can you see, the data source does not contain any element which is greater than 50, and hence when we run the below code, it will throw InvalidOperationException.

Output:

What Happens If the Specified Condition in the Last Method Does not Match with Any Element

Note: If you don’t want that Invalid Operation Exception, instead you want a default value based on the data type then you need to use the LINQ LastOrDefault method. For the reference types, the default value is NULL and for the value types, the default value depends on the actual data type, for example, for numeric data types it is 0, for string data types, it is null, and for boolean data type, it is False.

LINQ LastOrDefault Method in C#:

The LINQ LastOrDefault Method in C# exactly does the same thing as the LINQ Last method does except that this method does not throw the InvalidOperationException exception when the data source is empty or when the specified condition does not match with any element in the data source. In such cases, it will return the default value based on the data type of the data source. If the Data Source is Null, then like the Last Method, it will also throw ArgumentNullException. Like the Last method, there are also two overloaded versions available for the LastOrDefault method as shown below.

First Overloaded Version: It returns the last element of a sequence, or a default value if the sequence contains no elements.
First Overloaded Version: It returns the last element of the sequence that satisfies the given condition or a default value if no such element is found in the data source or if the data source is empty.

Example to Understand LINQ LastOrDefault Method in C#.

Let us see an example to Understand LINQ LastOrDefault Method in C# with both Method and Query Syntax. Our requirement is to fetch the last Element from the Data Source using LastOrDefault Method. For a better understanding, please have a look at the following example. Here we have created one data source which contains integer numbers from 1 to 10. Then we need to fetch the last element from the collection using the LastOrDefault method. There is no such operator called LastOrDefault available to write the Query Syntax, If you want then you can combine both the method syntax and query syntax to write the code which is also shown in the below example.

Output: 10

Example to Understand LINQ LastOrDefault Method which takes Predicate as a Parameter in C#

Now, we need to fetch the last element from the data source which is less than 5. Here we need to use the second overloaded version of the LastOrDefault method using which we can specify our condition. For a better understanding, please have a look at the following example. The following program uses the second overloaded version of the LINQ LastOrDefault to return the last element from the data source which is less than 5. See, there are many elements that satisfy the given condition, but the last element which satisfies the given condition is 4 which is going to be returned by the LastOrDefault method. In the below example, I am showing how to use the second overloaded version of the LINQ LastOrDefault method in C# using both Method and Query Syntax.

Output: 4

 

 

What Happens when we call the LastOrDefault Method on Empty Data Source or when the condition does not satisfy any element?

Whenever the data source is empty, then the LINQ LastOrDefault Method will not throw InvalidOperationException. Instead, it will return a default value based on the data type. Similarly, if no elements are found with the given condition then also it will not throw any exception. Instead, it will also return the default value based on the data type.

For a better understanding, please have a look at the following example. In the below example, we have created two data sources. The first data source does not contain any item and here we are trying the fetch the last element. The second data source contains 10 elements from the numbers 1 to 10. From this data source, we are trying to retrieve the last element which is greater than 50.

Here it will print the values as 0 and 0. This is because the data source contains integers. And the default for integers is 0.

LINQ Last and LastOrDefault Method with Complex Type in C#:

Let us see an example to Understand How to use LINQ Last and LastOrDefault Method with Complex Type in C#. For this, we are going to use the following Employee class. So, first, create a class file with the name Employee.cs and then copy and paste the following code into it. It is a very simple class having 4 properties and one method which is to return a collection of employees.

 

Example to Understand LINQ Last Method with Complex Type in C#:

The following example shows how to use the LINQ Last method (both overloaded versions) with Complex Type. The following Example code is self-explained, so please go through the comment lines for a better understanding.

 

Example to Understand LINQ LastOrDefault Method with Complex Type in C#:

The following example shows how to use the LINQ LastOrDefault method (both overloaded versions) with Complex Type. The following Example code is self-explained, so please go through the comment lines for a better understanding.

 

What is the Difference Between the Last and LastOrDefault Methods in LINQ?

Both Last and LastOrDefault methods in LINQ are used to return the last element from a data source. But if the data source is empty or if no element is satisfied with the specified condition, then the Last method will throw an exception while the LastOrDefault method will not throw an exception instead it returns a default value based on the data type of the element.

In the next article, I am going to discuss the LINQ Single and SingleOrDefault Methods with Examples. In this article, I try to explain the LINQ Last and LastOrDefault Methods in C# with Examples. I hope you understood the need and use of these Last and LastOrDefault methods in LINQ.

Leave a Comment