views:

251

answers:

4

I'm having a devil of a time finding a comparison of the different ways to query for a single item, and when to use each.

Does anyone have a link that compares all these, or a quick explanation as to why you would use one over the other? Are there still more operators that I am unaware of?

Thank you.

+3  A: 

It's really very simple: Single returns a single item and throw an exception if there is either none or more than one item. First will return the first item or throw when there is no item. FirstOrDefault will return the first item or return null (in most cases) when there is no item.

This is the behavior the API is supposed to have. Note however that the underlying implementation could have a different behavior. While Entity Framework obeys this, a O/RM like LLBLGen can also return null when calling First which is a very strange thing. This was a very strange (and stubborn) decision by the designer IMO.

Steven
Thanks Steven. I guess I'm still wondering why you'd use one over the other? I've always used FirstOrDefault(), and was curious as to why a lot of the new examples I've seen have switched to Single(). Is there a reason to switch to Single()? Are there others that also accomplish the same thing, that I should consider instead?
PolishedTurd
If you like your code to "fail fast", First() and Single() let your code more precisely say what is expected (so it can fail otherwise)
Frank Schwieterman
I totally agree with Frank. It’s also about communicating intent. `Single` expresses clearly that you only expect the result to have one element.
Steven
+2  A: 

You missed SingleOrDefault() which will get a single value, throwing an exception if there is more than one item returned but returning a default value (normally a null) if there is no item returned by your query.

Steve Willcock
Ah, thanks. Yet another that seemingly accomplishes the same thing - lol.
PolishedTurd
Do you use all 4 depending on the scenario, or stick to just one?
PolishedTurd
It depends on the scenario. If you know you should always get a single record back from the db, no more, no less, for a given query then Single() is the 'right' one to use. In other situations the others may be more appropriate. In previous versions of EF we were limited to First() and FirstOrDefault() which work for scenarios where you are expecting a single record but they won't warn you if you actually get more than that single record back which could be important depending on the situation.
Steve Willcock
Thanks. I can't see myself needing First() anymore, where Single() wouldn't be better. If I were less dense, I'm sure I could appreciate/understand when to use First() still.
PolishedTurd
+2  A: 

The four methods each have their place; Though you really only have two different operations.

  • First - Expecting a result set that contains multiple items, give me the first item in that set.
  • Single - Expecting a single result back, give me that item.

The xxxxOrDefault() version just adds on "I don't want to consider an empty result set to be an exceptional circumstance."

Chris Shaffer
OK, so it seems to me that First() would rarely come in handy. I'm having a hard time coming up with a scenario where Single() wouldn't be the first option. You have a quick one off hand, by chance? Thanks.
PolishedTurd
Unfortunately a lot of developers use First() or FirstOrDefault() purely as a defensive measure, thinking it will avoid an exception when it really just has the potential to hide real problems.
Matt
+2  A: 

I always tend to use FirstOrDefault. If you really want to be picky with performance then you should use FirstOrDefault in EF. Under the covers SingleOrDefault uses top (2) in the query because, it needs to check if there is a second row that matches the criteria and if it does, it throws an exception. Basically in SingleOrDefault you are saying that u care to throw an exception if your query returns more then 1 record.

zeeshanhirani