LINQ FirstOrDefault Method

In LINQ, the FirstOrDefault operator is used to return the first element from the list/collection, and it's same as LINQ First operator, but the only difference is in case if the list returns no elements, then the LINQ FirstOrDefault method will return the 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 the above syntax, we are trying to get the First element or default element from the “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 the first element from the list or a default value 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 the above example, we have two lists, “obList”, “objVals” and we are trying to get the first element or default elements from the lists by using LINQ FirstOrDefault() method.

Result LINQ FirstOrDefault() in Method Syntax Example

Following is the result of the LINQ FirstOrDefault() operator in the 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 the 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

Result of LINQ FirstOrDefault() in Query Syntax

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

 

Element from the List1: 1
Element from the List2: 0

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