LINQ LastOrDefault() Method

In LINQ, LastOrDefault() method/operator is used to return the last element from the list/collection, and it's same as LINQ Last() method, but only the difference is it will return the default value in case if the list/collection contains no element.

Syntax of LINQ LastOrDefault() Method

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

 

C# Code

 

int result = objList.LastOrDefault();

VB.NET Code

 

Dim result As Integer = objList.LastOrDefault()

If you observe the above syntax, we are trying to get the last or default element from the “objList” list using LINQ LastOrDefault() method.

Example of LINQ LastOrDefault in Method Syntax

Following is the syntax of using the LINQ LastOrDefault() operator in method syntax to get the last element from the list or default value.

 

C# Code

 

using System;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] objList = { 1, 2, 3, 4, 5 };
      int[] objVals = { };
      int result = objList.LastOrDefault();
      int val = objVals.LastOrDefault();
      Console.WriteLine("Element from the List1: {0}", result);
      Console.WriteLine("Element from the List2: {0}", val);
      Console.ReadLine();
    }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim objList As Integer() = {1, 2, 3, 4, 5}
Dim objVals As Integer() = {}
Dim result As Integer = objList.LastOrDefault()
Dim val As Integer = objVals.LastOrDefault()
Console.WriteLine("Element from the List1: {0}", result)
Console.WriteLine("Element from the List2: {0}", val)
Console.ReadLine()
End Sub
End Module

If you observe the above code, we are getting the last element or default from “objList” and “objVals” lists by using LINQ LastOrDefault() method.

Result of LINQ LastOrDefault() in Method Syntax

Following is the result of LINQ LastOrDefault() in method syntax example.

 

Element from the List1: 5
Element from the List2: 0

Example of LINQ LastOrDefault() in Query Syntax

Following is the example of the LINQ LastOrDefault() operator in query syntax to get the last element from the list or default value in case if list returns no elements.

 

C# Code

 

using System;
using System.Linq;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      int[] objList = { 1, 2, 3, 4, 5 };
      int[] objVals = { };
      int result = (from l in objList select l).LastOrDefault();
      int val = (from x in objVals select x).LastOrDefault();
      Console.WriteLine("Element from the List1: {0}", result);
      Console.WriteLine("Element from the List2: {0}", val);
      Console.ReadLine();
    }
  }
}

 VB.NET Code

 

Module Module1

Sub Main()
Dim objList As Integer() = {1, 2, 3, 4, 5}
Dim objVals As Integer() = {}
Dim result As Integer = (From l In objList).LastOrDefault()
Dim val As Integer = (From x In objVals).LastOrDefault()
Console.WriteLine("Element from the List1: {0}", result)
Console.WriteLine("Element from the List2: {0}", val)
Console.ReadLine()
End Sub
End Module

Result of LINQ LastOrDefault() in Query Syntax Example

Following is the result of the LINQ LastOrDefault() operator in the query syntax example.

 

Element from the List1: 5
Element from the List2: 0

This is how we can use the LINQ LastOrDefault() operator in method syntax and query syntax to get the last element from the list or a default value if the list contains no elements.