Selecting item from an IEnumerable
c#, linq
Solution
You can avoid this problem using `ToList()`. Becuase then when you have the list loaded in your memory you can do whatever you want. But I reccomend you use `FirstOrDefault()`.
var results = dataContext.loadData(testargument).ToList();
And
ReturnedEntity entity = results.FirstOrDefault();
Or
if(results.Length == 1)
ReturnedEntity entity = results.First();
Problem
Possible Duplicate: The query results cannot be enumerated more than once? I am using entity framework to select and return a group of entities form my database using a stored procedure. ``` var results = dataContext.loadData(testargument); ``` I want to count this returned set (to make sure only 1 record is returned and then take the first item in this list. ``` if(results.Count() == 1) { ReturnedEntity entity = results.First(); } ``` However when I do this call I get the error "The result of a query cannot be enumerated more than once." Does anyone know how I can do this correctly? I assume that calling the Count() method is changing the data and am not sure if I need to put it back into a list before calling the first() method. I have tried results.ToList().First() but get the same error. In addition I notice that if I call the First() method on an empty set I get an error which is why I am trying to make sure there is only 1 record returned.