LINQ First vs FirstOrDefault vs Single vs SingleOrDefault

Below is a chart explaining the difference between them and examples of each scenario.

First() FirstOrDefault() Single() SingleOrDefault()

Description Return first element of the sequence elements

Return first element of the sequence elements or default value if no element is found

Return a single element of a sequence


Return a single element of a sequence, or a default value if that element is not found\
Exception Thrown when There are no elements in the result Only if the source is null There are 0 or more than 1 elements in the result

There is more than one element in the result


When to use When more than 1 element is expected and you want only the first When more than 1 element is expected and you want only the first. Also it is ok for the result to be empty If exactly 1 element is expected not 0 or more than 1 When 0 or 1 element is expected




Example

First, we create an Employee table for querying. We will test with different scenarios


Emp_Id FirstName LastName BirthDay
1 Rahul Sharma 12/8/1948 12:00:00 AM
2 Virat Kohli 2/19/1952 12:00:00 AM
3 Yusuf Pathan 8/30/1963 12:00:00 AM
4 Sudeep Tyagi 9/19/1937 12:00:00 AM
5 RahulKumar 3/4/1955 12:00:00 AM
6 Umesh Yadav 7/2/1963 12:00:00 AM
7 Vinay Ojha 5/29/1960 12:00:00 AM
8 Pankaj Singh 1/9/1958 12:00:00 AM
9 Varun Aaron 1/27/1966 12:00:00 AM

1) First()

StatementEmployee.OrderBy(e => e. BirthDay).First(e => e.FirstName == "Rahul")
Expected Result:  There are multiple records where FirstName == Rahul. It will return the old one.
Actual Result
Emp_Id 5
FirstName Rahul
LastName Kumar
Birthdate 3/4/1955 12:00:00 AM

Statement: Employee.OrderBy(e => e. BirthDay).First(e => e.Emp_Id== 10)
Expected Result:There is no record with Employeeid = 10. It will fail.
Actual Result: InvalidOperationException: Sequence contains no elements

2) FirstOrDefault()

Statement: Employee.OrderBy(e => e. BirthDay) .FirstOrDefault(e => e.FirstName == "Rahul")
Expected Result:There are multiple records where FirstName == Rahul. It will return the old one. Actual Result:
Emp_Id 5
FirstName Rahul
LastName Kumar
Birthdate 3/4/1955 12:00:00 AM

Statement : Employee.OrderBy(e => e. BirthDay).FirstOrDefault(e => e.Emp_Id == 10)
Expected Result: There is no record with Empl_Id = 10. Should return the default value.
Actual Result: null

3) Single()

Statement: Employee: Single (e => e. Emp_Id == 1)
Expected Result: There is only one record where Emp_Id equal to 1
Actual Result:
Emp_Id1
FirstNameRahul
LastNameSharma
Birthdate12/8/1948 12:00:00

Statement: Employee.Single(e => e.FirstName == "Rahul")
Expected Result: There are multiple records where FirstName == Rahul it should fail.
Actual Result: InvalidOperationException: Sequence contains more than one element

Statement: Employee.Single(e => e.Emp_Id==10)
Expected Result: There is no record with Emp_Id equal to 10 it will fail
Actual Result: InvalidOperationException: Sequence contains no elements

4) SingleOrDefault()

Statement: Employee.SingleOrDefault(e => e.Emp_Id == 1) 
Expected Result: There is only one record where Emp_Id == 1.
Actual Result
Emp_Id 1
FirstName Rahul
LastName Sharma
Birthdate 12/8/1948 12:00:00 AM

Statement: Employee.SingleOrDefault(e => e.FirstName == "Rahul")
Expected Result: There are multiple records where FirstName == Rahul, it will fail.
Actual Result: InvalidOperationException: Sequence contains more than one element 

Statement: Employee.SingleOrDefault(e => e.Emp_Id == 10) 
Expected Result: There is no record with Emp_Id = 10 it will return the default value.
Actual Result: null

I hope this article is useful for you. Look forward for your comments and feedback. 

Comments

Popular posts from this blog

Introduction to MVC

Error when you browse your ASP site from IIS

Create Connection String Through Notepad