When to use Single, SingleOrDefault, First and FirstOrDefault
You should take care of following points while choosing Single, SingleOrDefault, First and FirstOrDefault:
- When you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
- When you want a default value is returned if the result set contains no record, use SingleOrDefault.
- When you always want one record no matter what the result set contains, use First or FirstOrDefault.
- When you want a default value if the result set contains no record, use FirstOrDefault.
Perfomance of SingleOrDefault and FirstOrDefault
FirstOrDefault usually perform faster as compared SingleOrDefault, since these iterate the collection until they find the first match. While SingleOrDefault iterate the whole collection to find one single match.