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.
- class Employee
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Department{ get; set; }
- }
I have a list of Employees:
- List<Employee> employeeList = new List<Employee>(){
- new Employee() { Id = 1, Name = "Sunny", Department = "Technical" },
- new Employee() { Id=2, Name="Pinki", Department ="HR"},
- new Employee() { Id=3, Name="Tensy", Department ="Finance"},
- new Employee() { Id=4, Name="Bobby", Department ="Technical"},
- new Employee() { Id=5, Name="Sweety", Department ="HR"}
- };
First()
- It returns the first element of a sequence.
- It throws an error when there is no element in the result, or source is null.
- We should use it if more than one element is expected and you want only the first element.
Ex 1
- var result = employeeList.First();
Will return
- Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Ex 2 - var result = employeeList.First(e=>e.Department== "HR");
Will return
- Employee() { Id=2, Name="Pinki", Department ="HR"}
Ex 3
- 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()
- It returns the first element of a sequence, or a default value if no element is found.
- It throws an error only if the source is null.
- 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
- var result = employeeList.FirstOrDefault();
Will return:
- Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2 - var result = employeeList.FirstOrDefault(e=>e.Department== "HR");
Will return:
- Employee() { Id=2, Name="Pinki", Department ="HR"}
Example 3
- var result = employeeList.FirstOrDefault(e=>e.Id == 8);
Will not throw an error but it returns default value of Employee.
Single()
- It returns the only item of a sequence.
- This will throw an exception if the result contains 0 or more than 1 elements.
- We should use it, when we know that exactly one element is expected but neither 0 nor 2 or more.
Example 1
- var result = employeeList.Single(e=e.Id==1);
Will return:
- Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2
- var result = employeeList.FirstOrDefault(e=>e.Department== "HR");
Will throw an exception as 2 employees exist with Department as "HR".
Example 3
- var result = employeeList.Single(e=>e.Id == 8);
Will throw an exception as no employee exists with Id as 8.
SingleOrDefault()
- It returns single specific element, and if the element is not found, it returns the default value of it.
- This will throw an exception if the result contains 2 or more elements.
- We should use it when we know that 0 or 1 element is expected as result.
Example 1
- var result = employeeList.SingleOrDefault(e=>e.Id == 1);
Will return
- Employee() { Id = 1, Name = "Sunny", Department = "Technical" }
Example 2 - var result = employeeList.SingleOrDefault(e=>e.Department== "HR");
Will throw an exception as 2 employees exist with Department as HR in employeeList.
Example 3
- 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.