First, FirstOrDefault, Single, SingleOrDefault In C#

For people who are new to LINQ, it is difficult to understand the difference between First, FirstOrDefault, Single, SingleOrDefault. In this blog, I will explain what to use and when. 
  
I will take a simple example to make you understand practically how these methods work.
 
Consider a class Employee with properties as Id, Name, and Department.
  1. class Employee
  2. {
  3.    public int Id { get; set; }
  4.    public string Name { get; set; }
  5.    public string Department{ get; set; }
  6. }
I have a list of Employees:
  1. List<Employee> employeeList = new List<Employee>(){
  2.    new Employee() { Id = 1, Name = "Sunny", Department = "Technical" },
  3.    new Employee() { Id=2, Name="Pinki", Department ="HR"},
  4.    new Employee() { Id=3, Name="Tensy", Department ="Finance"},
  5.    new Employee() { Id=4, Name="Bobby", Department ="Technical"},
  6.    new Employee() { Id=5, Name="Sweety", Department ="HR"}
  7. };
First()
  1. It returns the first element of a sequence.
  2. It throws an error when there is no element in the result, or source is null.
  3. We should use it if more than one element is expected and you want only the first element.
Ex 1
  1. var result = employeeList.First();
Will return
  1. Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Ex 2
  1. var result = employeeList.First(e=>e.Department== "HR");
Will return
  1. Employee() { Id=2, Name="Pinki", Department ="HR"}
Ex 3
  1. var result = employeeList.First(e=>e.Id == 8);
This will throw an error, because employee with Id as 8 does not exist in the employeeList.
 
FirstOrDefault()
  1. It returns the first element of a sequence, or a default value if no element is found.
  2. It throws an error only if the source is null.
  3. We should use it if more than one element is expected and you want only the first element. It's also good if the result is empty.
Example 1
  1. var result = employeeList.FirstOrDefault();
Will return:
  1. Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2
  1. var result = employeeList.FirstOrDefault(e=>e.Department== "HR");
Will return:
  1. Employee() { Id=2, Name="Pinki", Department ="HR"}
Example 3
  1. var result = employeeList.FirstOrDefault(e=>e.Id == 8);
Will not throw an error but it returns default value of Employee.
 
Single()
  1.  It returns the only item of a sequence.
  2. This will throw an exception if the result contains 0 or more than 2 elements.
  3. We should use it, when we know that exactly one element is expected but neither 0 nor 2 or more.
Example 1
  1. var result = employeeList.Single(e=e.Id==1);
Will return:
  1. Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2
  1. var result = employeeList.FirstOrDefault(e=>e.Department== "HR");
Will throw an exception as 2 employees exist with Department as "HR".
 
Example 3
  1. var result = employeeList.Single(e=>e.Id == 8);
Will throw an exception as no employee exists with Id as 8.
 
SingleOrDefault()
  1. It returns single specific element, and if the element is not found, it returns the default value of it.
  2. This will throw an exception if the result contains 2 or more elements.
  3. We should use it when we know that 0 or 1 element is expected as result.
Example 1
  1. var result = employeeList.SingleOrDefault(e=>e.Id == 1);
Will return
  1. Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2
  1. var result = employeeList.SingleOrDefault(e=>e.Department== "HR");
Will throw an exception as 2 employees exist with Department as HR in employeeList.
 
Example 3
  1. var result = employeeList.SingleOrDefault(e=>e.Id == 8);
Will not throw an exception but it returns the default value of Employee.
 
I hope you understand the difference between First, FirstOrDefault and  Single, SingleOrDefault.
 
Happy learning.
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now