In Linq, a very interesting topic is Difference between Single() or SingleOrDefault(), or we can say when to use Single() and when SingleOrDefault(), So, Today I am going to explain these two extension method in Linq.
Single()
It returns always a single value from collection of elements when any condition is not specified . If collection contain more than 1 element or If query is returning result is 0 or more than 1 than it throws the InvalidOperationException exception. Same as If conditions is more than one than it also throws the InvalidOperationException.
When to use Single()
When you know that output result should be contain exactly 1 element, neither 0 nor more than 1. Than you can use Single.
SingleOrDefault()
SingleOrDefault is just like Single() except that, if no element match the specified condition than it returns default value of underlying type of generic collection. It does not throws InvalidOperationException if no element found. But more than one element found for specified conditions than throws an exception.
When to use SingleOrDefault()
When you know that output result should contain 0 or 1 elements or if you want that default value should be returned if no element is found.
Example
Use below example to one by one to comment others.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LinqSingleorSingleOrDefault
{
class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
public class Program
{
static void Main(string[] args)
{
IList<Employee> employeeList = new List<Employee>(){
new Employee() { Id = 10, Name = "Chris", City = "London" },
new Employee() { Id=11, Name="Robert", City="London"},
new Employee() { Id=12, Name="Mahesh", City="India"},
new Employee() { Id=13, Name="Peter", City="US"},
new Employee() { Id=14, Name="Chris", City="US"}
};
//Single Example
var result1 = employeeList.Single();
// this will throw an InvalidOperationException exception because more than 1 element in employeeList.
var result2 = employeeList.Single(e => e.Id == 11);
//exactly one element exists for Id=11
var result3 = employeeList.Single(e => e.Name == "Chris");
// throws an InvalidOperationException exception because of more than 1 element contain for Name=Chris
IList<int> intList = new List<int> { 2 };
var result4 = intList.Single();
// return 2 as output because exactly 1 element exists
//SingleOrDefault Example
var result5 = employeeList.SingleOrDefault(e => e.Name == "Mohan");
//return default null because not element found for specific condition.
var result6 = employeeList.SingleOrDefault(e => e.Name == "Chris");
// throws an exception that Sequence contains more than one matching element
var result7 = employeeList.SingleOrDefault(e => e.Id == 12);
//return only 1 element
Console.ReadLine();
}
}
}
Performance
SingleOrDefault is slower than FirstOrDefault because of SingleOrDefault search element in whole collection of element but FirstOrDefault find the first match.
Conclusion:
Today We learned what is difference between Single() and SingleOrDefault() extention method in Linq and when to use both of these.
I hope this post will help you. Please put your feedback using comment which helps me to improve myself for next post. If you have any doubts please ask your doubts or query in the comment section and If you like this post, please share it with your friends. Thanks.