LINQ LastOrDefault() Method

  Total Views : 176
  Comments : 0
 
  Keywords : Linq lastordefault() element operator with example in c# / vb.net, Use linq lastordefault() method to get last element or default element from the collection in c# / vb.net with example

Here we will learn LINQ LastOrDefault() method in c#, vb.net with example and how to use LINQ LastOrDefault() in method syntax (lambda) and query syntax to get last element from the list or default value in case if the list / collection returns null values.

LINQ LastOrDefault() Method

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

Syntax of LINQ LastOrDefault() Method

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

 

C# Code

 

int result = objList.LastOrDefault();

VB.NET Code

 

Dim result As Integer = objList.LastOrDefault()

If you observe above syntax we are trying to get 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 LINQ LastOrDefault() operator in method syntax to get 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 above code we are getting last element or default from “objList” and “objVals” lists by using LINQ LastOrDefault() method.

Output 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 LINQ LastOrDefault() operator in query syntax to get 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

Output of LINQ LastOrDefault() in Query Syntax Example

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

 

Element from the List1: 5

Element from the List2: 0

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

 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User