LINQ Single() Method

  Total Views : 201
  Comments : 0
 
  Keywords : Linq single element operator method in c# / vb.net with example, Linq single() extension method in c# / vb.net with example, Use linq single() method to get single element from collection

Here we will learn LINQ Single() method in c#, vb.net with example and how to use LINQ Single() method to get single element from collection or raise invalidoperationexception error in case if list returns more than one element in c#, vb.net with example.

LINQ Single() Method

In LINQ Single operator is used to return single element from the collection or single element that satisfy the condition. In case if Single() method found more than one element in collection or no elements in collection then it will throw "InvalidOperationException" error.

Syntax of LINQ Single() Method

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

 

C# Code

 

int val = objList.Single();

VB.NET Code

 

Dim val AsInteger = objList.[Single]()

If you observe above syntax we are getting single element from the list using LINQ Single() method.

Example of LINQ Single() Method

Following is the example of LINQ Single() method to get single element from the collection.

 

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 };

var user = objStudent.Single(s => s.Name == "Suresh Dasari");

string result = user.Name;

int val = objList.Single();

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}

Dim user = objStudent.[Single](Function(s) s.Name = "Suresh Dasari")

Dim result As String = user.Name

Dim val As Integer = objList.[Single]()

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 getting single element from collection "objStudent" using LINQ Single() operator.

Output of LINQ Single() Method Example

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

 

Element from objStudent: Suresh Dasari

Element from objList: 1

As we discussed in case if list returns more than one value then it will throw "InvalidOperationException" using LINQ Single() operator. We will see this with example.

LINQ Single() Operator Exception Example

Following is the example which will throw "InvalidOperationException" error because Single() method found multiple result elements.

 

C# Code

 

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 };

// It will throw InvalidOperationException Exception because of Multiple Values

var user = objStudent.Single();

string result = user.Name;

// It will throw InvalidOperationException Exception because of Multiple Values

int val = objList.Single();

Console.WriteLine("Element from objStudent: {0}", result);

Console.WriteLine("Element from objList: {0}", val);

Console.ReadLine();

VB.NET Code

 

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}

' It will throw InvalidOperationException Exception because of Multiple Values

Dim user = objStudent.[Single]()

Dim result As String = user.Name

' It will throw InvalidOperationException Exception because of Multiple Values

Dim val As Integer = objList.[Single]()

Console.WriteLine("Element from objStudent: {0}", result)

Console.WriteLine("Element from objList: {0}", val)

Console.ReadLine()

If you observe above example it will throw "InvalidOperationException" error because Single() method will get multiple elements as result.

 

This is how we can use LINQ SIngle() method to get single element from the collection or raise exception in case if list / collection returns more than one element.

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User