Paulo Morgado

.NET Development & Architecture

Recent Articles

view all

Events

Projects

Recent Readers

Visitor Locations

Visitor Locations

Disclaimer

The opinions and viewpoints expressed in this site are mine and do not necessarily reflect those of Microsoft, my employer or any community that I belong to. Any code or opinions are offered as is. Products or services mentioned are purchased by me, made available to me by my employer or the manufacturer/vendor which doesn't influence my opinion in any way.

LINQ: Single vs. First

I’ve witnessed and been involved in several discussions around the correctness or usefulness of the Single method in the LINQ API.

The most common argument is that you are querying for the first element on the result set and an exception will be thrown if there’s more than one element. The First method should be used instead, because it doesn’t throw if the result set has more than one item.

Although the documentation for Single states that it returns a single, specific element of a sequence of values, it actually returns THE single, specific element of a sequence of ONE value. When you use the Single method in your code you are asserting that your query will result in a scalar result instead of a result set of arbitrary length.

On the other hand, the documentation for First states that it returns the first element of a sequence of arbitrary length.

Imagine you want to catch a taxi. You go the the taxi line and catch the FIRST one, no matter how many are there.

On the other hand, if you go the the parking lot to get your car, you want the SINGLE one specific car that’s yours. If your “query” “returns” more than one car, it’s an exception. Either because it “returned” not only your car or you happen to have more than one car in that parking lot. In either case, you can only drive one car at once and you’ll need to refine your “query”.

Comments

Arun Mahendrakar said:

Nice explanation Paulo.

Arun

# March 29, 2010 12:18 AM

safknw said:

Thanks for useful tip.

# March 29, 2010 1:00 AM

ASP.NET MvcPager said:

how about SingleOrDefault()??

# March 29, 2010 8:58 AM

Cristovao Morgado said:

And why not use SingleOrDefault?

Testing a Null is "cheaper" than try - catch

# March 29, 2010 12:53 PM

Paulo Morgado said:

Tao anda Cristovão,

Does this answer your question?

paulomorgado.net/.../linq-single-vs-singleordefault.aspx

# March 29, 2010 8:45 PM

Paulo Morgado said:

Like other LINQ API methods that extract a scalar value from a sequence, Single has a companion SingleOrDefault.

# March 29, 2010 8:48 PM

Nilesh said:

Nice Explaination !! Thanks

# April 4, 2010 7:35 AM

Abdel Olakara said:

Good article about Single & First method! Thanks

# May 19, 2010 8:32 AM

Paulo Morgado said:

Thanks, Abdel.

# May 19, 2010 7:56 PM

Stephan said:

Nice post. One small comment. I'm not sure that 'scalar' is the most appropriate term in that context. 'Element' would be a better one probably.

# May 20, 2010 12:02 PM

Paulo Morgado said:

I thought of "item" but it still didn't sound clear enough to me. Than I thouht of "scalar" as oposed to "set". I find that it's hard for some people to diferentiate one element from a set of one element.

# May 20, 2010 5:58 PM

Sean said:

SingleOrDefault should only be used when it's ok that null is an acceptable value, Single should be used when you are 100% expecting a single item back.

# August 20, 2010 9:43 AM

Paulo Morgado said:

Actually, SingleOrDefault should only be used when it's expected that result set might be empty and the default value (not necessarily null) is accepted instead.

# August 20, 2010 8:09 PM

arneb said:

If you for example are basing your where on a primary key in a DB then you will only get one element then I'd say use Singel, and let it throw a exception if you get more then one element in return, because then something is very wrong, and you want to know about that exception, instead of maybe not noticing it with the use of first.

# September 20, 2010 8:48 AM

Paulo Morgado said:

You are being too restrictive, arneb.

The use case you're referring to is just one of many. This one is enforced by the DB. But there can be much more cases (application logic, no-SQL stores, etc.).

But your bottom line is correct. Something went wrong - fail fast.

# September 20, 2010 10:05 AM

AndrewSeven said:

Single can be slower too, as it must go through the entire list to be sure that there is only one match; so if you don't need the guarantee, just use First

# May 2, 2011 7:20 PM

Paulo Morgado said:

Andrew,

The point here is that they have different purposes.

You use Single (which doesn't go through the entire list) it's because there sould be only one element. If there's more than one element, sure there will be some extra work moving to the next element, but that means something else went wrong before.

# May 2, 2011 7:24 PM

Paulo Morgado said:

Single only iterates over one more element than First.

The only time Single enumerates the entire collection is when the collection has only two elements.

Take a look at Jon Skeet's Edulinq post about First/Single/Last and the ...OrDefault (msmvps.com/.../reimplementing-linq-to-objects-part-11-first-single-last-and-the-ordefault-versions.aspx)

# July 3, 2011 5:45 PM

Sanjana said:

i am facing one problem with the first.

the result set has so many records.

If i use the first it is returning only some records.

How to get all the records?

Thanks in advance

# October 18, 2011 1:49 AM

Paulo Morgado said:

Sanjana, I'm sorry but I don't understand your problem.

First is supposed to return only one record. Can you elaborate a bit more on your problem?

# October 18, 2011 4:06 AM

Pedro Adão said:

Thanks for this post!

Pedro

# December 3, 2012 7:58 AM