LINQ First vs FirstOrDefault vs Single vs SingleOrDefault
Below is a chart explaining the difference between them and examples of each scenario.
Example
First, we create an Employee table for querying. We will test with different scenarios
BirthDay ).First(e => e.FirstName == "Rahul")
Expected Result: There are multiple records where FirstName == Rahul. It will return the old one.
Actual Result
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)
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:
Expected Result: There is no record with Empl _Id = 10. Should return the default value.
3) Single
Statement: Employee: Single (e => e. Emp_Id == 1)
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
4)
Statement: Employee.SingleOrDefault(e => e.Emp_Id == 1)
Expected Result: There is only one record where Emp_Id == 1.
Actual Result
Statement: Employee.SingleOrDefault(e => e.FirstName == "Rahul")
Expected Result: There are multiple records where F irstName == Rahul, it will fai l.
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.
| First() | Single |
|||
|---|---|---|---|---|
| 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 | |||
|---|---|---|---|
| 1 | Rahul | Sharma | 12/8/1948 12:00:00 AM |
| 2 | 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 | Rahul | Kumar | 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()
Statement: Employee.OrderBy(e => e.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.
Expected Result
Actual Result: InvalidOperationException: Sequence contains no elements
2) FirstOrDefault ()
Statement: Employee.OrderBy(e => e. Expected 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)
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:
Actual Result:
| Emp_Id | 1 |
| Rahul | |
| Sharma | |
| 12/8/1948 12:00:00 |
Statement: Employee
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 |
| | Rahul |
| | Sharma |
| | 12/8/1948 12:00:00 AM |
Statement: Employee.SingleOrDefault(e => e.
Expected Result: There are multiple record
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
Post a Comment