Answered by:
Difference between Single, SingleOrDefault, First and FirstOrDefault

Question
-
User1183902823 posted
http://www.dotnettricks.com/learn/linq/understanding-single-singleordefault-first-and-firstordefault
Single
It returns a single specific element from a collection of elements if element match found. An exception is thrown, if none or more than one match found for that element in the collection.
SingleOrDefault
It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one match found for that element in the collection. A default value is returned, if no match is found for that element in the collection.
in case of single they said if no data found or more match found then exception will be thrown....that i understand but in case of singleordefault
tell me when exception will be thrown? A default value is returned, if no match is found....what would be the default value ? null
null will be return when no match found ?
First
It returns first specific element from a collection of elements if one or more than one match found for that element. An exception is thrown, if no match is found for that element in the collection.
FirstOrDefault
It returns first specific element from a collection of elements if one or more than one match found for that element. A default value is returned, if no match is found for that element in the collection.
thanks
Thursday, December 14, 2017 5:38 PM
Answers
-
User2103319870 posted
singleordefault tell me when exception will be thrown?
SingleorDefault will throw an exception when there is more than one element in the sequence.
For ex Consider you are using SingleOrDetfault like below code
string[] fruits1 = { "orange" }; string fruit1 = fruits1.SingleOrDefault();
Above code will work fine since array has only one value. However if your array contains more than one value like below
string[] fruits1 = { "orange","Apple" }; string fruit1 = fruits1.SingleOrDefault();
Then code will exception " Sequence contains more than one element". To resolve this either we need to add where condition clause to make sure we will return a single value as result or use FirstorDefault which will return the first value of sequence
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 14, 2017 8:06 PM -
User2103319870 posted
if there is no value then what SingleOrDefault will return ?
As mentione by mgebhard, return value depends on the data type's default value.
For example if you are working with string collection then SingleorDefault will return null
string[] fruits1 = { }; string fruit1 = fruits1.SingleOrDefault(); // Return null
if you are working with int then SingleorDefault will return 0
int[] intary = { }; int intval = intary.SingleOrDefault(); // returns zero
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 4:57 PM -
User1120430333 posted
if there is no value then what SingleOrDefault will return ?
If there is a single object in the collection, then the object with value is returned. If there is no object in the collection, then a null valued object is returned, which is the default value. That is why you should use the null value check to determine if an object is returned or not returned.
If for sure you know that there is ever only going to be one object in the collection and there is no way a single object will not be returned, then you can do this and not even use a loop to address the single object in the collection.
var name = authors.single().Name;
you can do the same thing with the First() to get the First object returned in a collection.
var name = authors.first().Name;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 10:59 PM
All replies
-
User475983607 posted
tell me when exception will be thrown? A default value is returned, if no match is found....what would be the default value ? null
null will be return when no match found ?
The default value depends on the type. For example, the default value for an int is 0. See the C# programming guide.
Thursday, December 14, 2017 7:53 PM -
User2103319870 posted
singleordefault tell me when exception will be thrown?
SingleorDefault will throw an exception when there is more than one element in the sequence.
For ex Consider you are using SingleOrDetfault like below code
string[] fruits1 = { "orange" }; string fruit1 = fruits1.SingleOrDefault();
Above code will work fine since array has only one value. However if your array contains more than one value like below
string[] fruits1 = { "orange","Apple" }; string fruit1 = fruits1.SingleOrDefault();
Then code will exception " Sequence contains more than one element". To resolve this either we need to add where condition clause to make sure we will return a single value as result or use FirstorDefault which will return the first value of sequence
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, December 14, 2017 8:06 PM -
User1183902823 posted
if there is no value then what SingleOrDefault will return ?
Friday, December 15, 2017 11:22 AM -
User2103319870 posted
if there is no value then what SingleOrDefault will return ?
As mentione by mgebhard, return value depends on the data type's default value.
For example if you are working with string collection then SingleorDefault will return null
string[] fruits1 = { }; string fruit1 = fruits1.SingleOrDefault(); // Return null
if you are working with int then SingleorDefault will return 0
int[] intary = { }; int intval = intary.SingleOrDefault(); // returns zero
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 4:57 PM -
User1120430333 posted
if there is no value then what SingleOrDefault will return ?
If there is a single object in the collection, then the object with value is returned. If there is no object in the collection, then a null valued object is returned, which is the default value. That is why you should use the null value check to determine if an object is returned or not returned.
If for sure you know that there is ever only going to be one object in the collection and there is no way a single object will not be returned, then you can do this and not even use a loop to address the single object in the collection.
var name = authors.single().Name;
you can do the same thing with the First() to get the First object returned in a collection.
var name = authors.first().Name;
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, December 15, 2017 10:59 PM