LINQ SingleOrDefault Method

  Total Views : 194
  Comments : 0
 
  Keywords : Singleordefault method in linq with example in c# / vb.net, Linq singleordefault method to get single element or default value in c# / vb.net, Linq singleordefault element operator example

Here we will learn LINQ SingleOrDefault() method in c#, vb.net with example and how to use LINQ SingleOrDefault() method to get single element or default value in case if no element present in list / collection.

LINQ SingleOrDefault() Method

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

Syntax of LINQ SingleOrDefault() Example

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

 

C# Code

 

int val = objList.SingleOrDefault();

VB.NET Code 

 

Dim val As Integer = objList.SingleOrDefault()

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

Example of LINQ SingleOrDefault Method

Following is the example of LINQ SingleOrDefault method to get single element from collection or default value in case if 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 above example we are trying to get single element from two list (objStudent, objList) objects by using LINQ SingleOrDefault method.

Output of LINQ SingleOrDefault() Method Example

Following is the result of 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 exception because 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 above example it will throw InvalidOperationException error because objList returns more than one value.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User