Wednesday, December 6, 2017

Differences between Single vs SingleOrDefault and First vs FirstOrDefault and Last vs LastOrDefault?

Differences between Single vs SingleOrDefault and First vs FirstOrDefault and Last vs LastOrDefault?


Difference between Single vs SingleOrDefault?
Single: Returns the element based on the given condition. If the condition matches for more than one element then it throws Invalid operation exception.
SingleOrDefault: Returns the element based on the given condition and returns a default value if no matches found. If the condition matches for more than one element then it throws Invalid operation exception.

Difference between First vs FirstOrDefault?
First: Returns first element based on the given condition. 
FirstOrDefault: Returns first element based on the given condition and returns a default value if no matches found.

Difference between Last vs LastOrDefault?
Last: Returns Last element based on the given condition. 
LastOrDefault: Returns Last element based on the given condition and returns a default value if no matches found.

No comments:

Post a Comment