SingleOrDefault and FirstOrDefault are element operators in LINQ. You might have come across the term ‘element operators’ quite a lot while working with C# or while browsing through resources related queries. So what exactly are Element Operators and why are they used?
What are Element Operators
Let us understand better with a simple example –
Let us consider a family of 4 – a father, a mother, a son and a daughter. As we can see, each member in this family has a specific identity i.e. if we wish to retrieve the entry for father, there will be only a single unique record. So, this retrieval of single records is done using Element Operators.
So, in technical terms, Element Operators are used to deal with single or multiple data sources to retrieve or return a single element based on a specified condition or the index of that element. Element operators can also be used to fetch the first or the last record from the data source.
In this article, we discuss 2 types of Element Operators and the differences between them in detail. They are –
- SingleOrDefault
- FirstOrDefault
SingleOrDefault
SingleOrDefault, as the name suggests, returns a single element from the collection. If the sequence is empty or if the element is not found, it returns a default value. An exception is thrown if the result does not contain exactly one element i.e. if the result includes more than one elements.
Referring to the above example, if the query is to retrieve the male entries from the family, the result will contain 2 elements i.e. the father and the son. In this case, SingleOrDefault will throw an exception.
Here is an example of SingleOrDefault:
FamilyMember fm =
db.Family.SingleOrDefault(f ⇒f.Role.Equals("father"));
FirstOrDefault
FirstOrDefault fetches either the first element from the collection or the first element that satisfies the given condition. A default value is returned if no such element is found in the collection. An exception is thrown if the collection does not exist.
In the above family example, if the entries are stored by age according to which the first entry is father followed by mother, son and daughter. The result will return the record for father.
Here is an example of FirstOrDefault:
FamilyMember fm =
db.Family.FirstOrDefault(f ⇒f.Role.Equals("father"));
How do we know when to use SingleOrDefault and when to use FirstOrDefault?
Here are the main differences between SingleOrDefault and FirstOrDefault that will help you decide which to use when –
SingleOrDefault | FirstOrDefault |
If the query returns multiple elements, SingleOrDefault will throw an exception. | If the query returns multiple elements, FirstOrDefault will return the first record from the lot. |
SingleOrDefault verifies whether there is exactly one element or not and hence iterates through the entire collection. Therefore, performance wise it is slower. | FirstOrDefault is usually faster performance wise since it iterates only until it fetches the very first element. |
SingleOrDefault is practically used most of the times when 0 or 1 elements are expected. | FirstOrDefault is usually applied when multiple results as well as no results are acceptable. |
Conclusion
Use FirstOrDefault if performance is your priority and uniqueness of the element is not a crucial factor whereas SingleOrDefault can be used in cases when the readability and stability of the code are more important than the performance.