LINQ FirstOrDefault Method

  Total Views : 218
  Comments : 0
 
  Keywords : Linq firstordefault() operator with example, How to use linq firstordefault() method to return first element or default value from the list / collection in c# / vb.net with example

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

LINQ FirstOrDefault Method

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

Syntax of LINQ FirstOrDefault Method

Following is the syntax of using LINQ FirstOrDefault() operator return first element from the list or default value in case if list returns no elements.

 

C# Code

 

int result = objList.FirstOrDefault();

VB.NET Code

 

Dim result As Integer = objList.FirstOrDefault()

If you observe above syntax we are trying to get First element or default element from “objList” collection by using  LINQ FirstOrDefault() operator.

Example of LINQ FirstOrDefault in Method Syntax

Following is the example of using LINQ FirstOrDefault in method syntax to return first element from the list or default value in case if the list contains 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 = objList.FirstOrDefault();

int val = objVals.FirstOrDefault();

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.FirstOrDefault()

Dim val As Integer = objVals.FirstOrDefault()

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 example we have two lists “obList”, “objVals” and we are trying to get first element or default elements from the lists by using LINQ FirstOrDefault() method.

Output of LINQ FirstOrDefault() in Method Syntax Example

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

 

Element from the List1: 1

Element from the List2: 0

Example of LINQ FirstOrDefault in Query Syntax

Following is the example of using LINQ FirstOrDefault() operator in query syntax.

 

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).FirstOrDefault();

int val = (from x in objVals

select x).FirstOrDefault();

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).FirstOrDefault()

Dim val As Integer = (From x In objVals).FirstOrDefault()

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 FirstOrDefault() in Query Syntax

Following is the result of using LINQ FirstOrDefault() operator in query syntax.

 

Element from the List1: 1

Element from the List2: 0

This is how we can use LINQ FirstOrDefault() operator in method syntax and query syntax to get first element from the list or default value from the list in case if no elements found in c#, vb.net

 
 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User