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 would recommend using
SingleOrDefaultinstead ofFirstOrDefault.This means that if there are more than one match, only the first one found will be returned.
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.