In LINQ, a Single operator returns a single element from the collection or a single element that satisfies the condition. If the Single() method finds more than one element in the collection or no elements in the collection, it will throw the "InvalidOperationException" error.
Syntax of LINQ Single() Method
Following is the syntax of using the LINQ Single() method to get a single element from the collection.
C# Code
int val = objList.Single();
VB.NET Code
Dim val AsInteger = objList.[Single]()
If you observe the above syntax, we get a single element from the list using the LINQ Single() method.
Example of LINQ Single() Method
The following is the LINQ Single() method to get a 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
The above example shows that we get a single element from the collection "objStudent" using LINQ Single() operator.
Result of LINQ Single() Method Example
Following is the result of the LINQ Single() method example.
Element from objStudent: Suresh Dasari
Element from objList: 1
As we discussed, if the list returns more than one value, it will throw "InvalidOperationException" using the LINQ Single() operator. We will see this with an example.
LINQ Single() Operator Exception Example
Following is the example which will throw the "InvalidOperationException" error because the 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}
Dim user = objStudent.[Single]()
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()
If you observe the above example, it will throw an "InvalidOperationException" error because the Single() method will get multiple elements as a result.
This is how we can use the LINQ SIngle() method to get a single element from the collection or raise exceptions if the list/collection returns more than one element.