Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# Querying With LINQ Query Operators Element Operators

Simon Coates
Simon Coates
28,694 Points

Is the description of SingleOrDefault incorrect?

the transcript currently includes "You might think, well, I should use SingleOrDefault so that I don't get an exception.". This would seem to imply that using the OrDefault version doesn't throw the exception. But the docs for SingleOrDefault include the text "Returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if there is more than one element in the sequence.". Is this a mistake or have I gotten myself tangled somehow? Carling Kirk

Simon Coates
Simon Coates
28,694 Points

to be clear, the quote seems to be in reference to duplicate behavior, which seems to be the same between the two versions of the methods.

2 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

Simon,

I agree that the statement "You might think, well, I should use SingleOrDefault so that I don't get an exception." in its given context is confusing. You're correct in that the SingleOrDefault method will still throw an exception if more than one match is found.

Steven is also correct in suggesting that FirstOrDefault is the "safest" method to use when attempting to retrieve a single record.

Choosing which method to use really depends upon what you're trying to achieve. Should the query always find a record? Should the query only ever find a single record? If it doesn't find a record or more than one record is found do you want that situation to be treated as an "exceptional" event (i.e. throw an exception)?

Thanks ~James

Simon Coates
Simon Coates
28,694 Points

James, Thank you for the response. I had assumed Steven's response was correct (he seems to have little use for being incorrect), but he didn't get into the language of the tutorial. It's reassuring to know that there was some confusion and it wasn't just that I was interpreting things strangely.

Steven Parker
Steven Parker
231,268 Points

Perhaps you want a different method.

If you want to avoid an error, but there's a possibility that there could be more than one record, you might use FirstOrDefault instead.

I'd guess that in the place SingleOrDefault was being used in the video, that there could only be one record or none. And a second record would indicate improper data, in which case the error would be desirable.

Simon Coates
Simon Coates
28,694 Points

thanks for the response. Good to know about FirstOrDefault. The context was as follows: "When we use Single, we want to make sure that there's only one of its kind in the collection. You might think, well, I should use SingleOrDefault so that I don't get an exception. It depends on what the situation is." (about the 3 min mark). So to me that implies that puts a discussion of different exception behaviour in terms of ensure there's "only one" (different to "exactly one"). Her later discussion (around 6.40) of the single method include mention of exceptions in multiple scenarios, while her subsequent slide on SingleOrDefault (6.43) doesn't seem to mention exception behaviour. I can't tell if the video is generally confusing, merely confusing to me (in whatever current state I occupy) or reflects a memory lapse on the part of Carling Kirk .

Steven Parker
Steven Parker
231,268 Points

It's been a while since I took that course, but I probably didn't expect to see exception behavior mentioned each time. And, depending on the data each method is used with, it may not be significant in the example.