IEnumerable.Single is throwing an exception

Someone asked on Stack Overflow:

I have the following code which uses Entity Framework:

g_getWidgets = from getWidgets in g_libraryEntities.GET_Widgets() select getWidgets;
.
.
.
IQueryable<GET_Fragments_Result> iqueryable = g_getWidgets.AsQueryable<GET_Widgets_Result>();
var nameValueObject = from nv in iqueryable where nv.ID == int.Parse(key) select nv;
widget = nameValueObject.Single();

The widget = nameValueObject.Single(); line throws an exception saying “The result of a query cannot be enumerated more than once.

What is the proper way to perform this function? I just want to to return an item with the proper ID.

I posted the following answer, which was chosen as the accepted answer and received 3 upvotes:

I would recommend using SingleOrDefault instead of FirstOrDefault.

Enumerable.FirstOrDefault()

Returns the first element of a sequence, or a default value if the sequence contains no elements.

Source: http://msdn.microsoft.com/en-us/library/bb340482.aspx

This means that if there are more than one match, only the first one found will be returned.

Enumerable.SingleOrDefault()

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.

Source: http://msdn.microsoft.com/en-us/library/bb342451.aspx

This means that if there is more than one match an exception is thrown. This is useful if having duplicate entries is a data violation.


Originally posted on Stack Overflow — 3 upvotes (accepted answer). Licensed under CC BY-SA.

signed letter b

Dad. Geek. Gamer. Software developer. Cloud user. Old Car enthusiast.  Blogger.


Top Posts


profile for Nate on Stack Exchange, a network of free, community-driven Q&A sites
a proud member of the blue team of 512KB club
Thoughts, opinions, and ideas shared here are my own. © 2026 Nate Bross.