The query results cannot be enumerated more than once
I received the error message ‘The query results cannot be enumerated more than once’ when I tried to get the count() of a collection twice or when I convert the object to List using ToList() method. I was using linq to sql for data access. So, the result object is of type ISingleResult<sp_GetEmployees>.
using (MyDataContext dataContext = GetDataContext())
var employees = dataContext.sp_GetEmployees;
var myList = employees.ToList();
The solution for this issue is,
a. Create a generic list and pass the result object as a parameter,
List<sp_GetEmployees> employeesList = new List<sp_GetEmployees>(employees);
b. Now, you can get the count or access the records in employees list.
No comments yet.