LINQ SingleOrDefault Method

In LINQ, the SingleOrDefault operator is used to return a single element or default value if there are no elements present in the list/collection. In case if the list/collection returns more than one element, it will throw an exception like a Single() method

Syntax of LINQ SingleOrDefault() Example

Following is the syntax of using the LINQ SingleOrDefault() method to get a single element from the collection.

 

C# Code

 

int val = objList.SingleOrDefault();

VB.NET Code 

 

Dim val As Integer = objList.SingleOrDefault()

If you observe the above syntax, it will return a single element from the collection “objList” or if the collection contains no elements, it will return the default value.

Example of LINQ SingleOrDefault Method

Following is the example of the LINQ SingleOrDefault method to get a single element from a collection or a default value if the collection contains no elements.

 

C# Code

 

using System;
using System.Linq;
using System.Collections.Generic;

namespace LINQExamples
{
  class Program
  {
    static void Main(string[] args)
    {
      List<Student> objStudent = new List<Student>()
      {
        new Student() { Name = "Suresh Dasari", Gender = "Male",Location="Chennai" },
        new Student() { Name = "Rohini Alavala", Gender = "Female", Location="Chennai" },
        new Student() { Name = "Praveen Alavala", Gender = "Male",Location="Bangalore" },
        new Student() { Name = "Sateesh Alavala", Gender = "Male", Location ="Vizag"},
        new Student() { Name = "Madhav Sai", Gender = "Male", Location="Nagpur"}
      };
      int[] objList = { 1,2,3,4,5 };
      var user = objStudent.SingleOrDefault(i => i.Name == "Suresh Dasari");
      string result = user.Name;
      int val = objList.SingleOrDefault(j=>j>5);
      Console.WriteLine("Element from objStudent: {0}", result);
      Console.WriteLine("Element from objList: {0}", val);
      Console.ReadLine();
    }
  }
  class Student
  {
    public string Name { get; set; }
    public string Gender { get; set; }
    public string Location { get; set; }
  }
}

VB.NET Code

 

Module Module1

Sub Main()
Dim objStudent As New List(Of Student)() From {
New Student() With {.Name = "Suresh Dasari", .Gender = "Male", .Location = "Chennai"},
New Student() With {.Name = "Rohini Alavala", .Gender = "Female", .Location = "Chennai"},
New Student() With {.Name = "Praveen Alavala", .Gender = "Male", .Location = "Bangalore"},
New Student() With {.Name = "Sateesh Alavala", .Gender = "Male", .Location = "Vizag"},
New Student() With {.Name = "Madhav Sai", .Gender = "Male", .Location = "Nagpur"}
}
Dim objList As Integer() = {1, 2, 3, 4, 5}
Dim user = objStudent.SingleOrDefault(Function(s) s.Name = "Suresh Dasari")
Dim result As String = user.Name
Dim val As Integer = objList.SingleOrDefault(Function(i) i > 5)
Console.WriteLine("Element from objStudent: {0}", result)
Console.WriteLine("Element from objList: {0}", val)
Console.ReadLine()
End Sub
Class Student
Public Property Name() As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Private m_Name As String
Public Property Gender() As String
Get
Return m_Gender
End Get
Set(ByVal value As String)
m_Gender = value
End Set
End Property
Private m_Gender As String
Public Property Location() As String
Get
Return m_Location
End Get
Set(ByVal value As String)
m_Location = value
End Set
End Property
Private m_Location As String
End Class
End Module

If you observe the above example, we are trying to get a single element from two lists (objStudent, objList) objects using the LINQ SingleOrDefault method.

Output of LINQ SingleOrDefault() Method Example

Following is the result of the LINQ  SingleOrDefault() method example.

 

Element from objStudent: Suresh Dasari
Element from objList: 0

The LINQ SingleOrDefault() method will throw InvalidOperationException error in case if the list / collection returns more than one element.

LINQ SingleOrDefault() Method Example with Exception

Following is the example which will throw an exception because the collection will return more than one element.

 

C# Code

 

int[] objList = { 1,2,3,4,5 };
int val = objList.SingleOrDefault();
Console.WriteLine("Element from objList: {0}", val);
Console.ReadLine();

VB.NET Code

 

Dim objList As Integer() = {1, 2, 3, 4, 5}
Dim val As Integer = objList.SingleOrDefault()
Console.WriteLine("Element from objList: {0}", val)
Console.ReadLine()

If we run the above example, it will throw an InvalidOperationException error because objList returns more than one value.