JG Vimalan's Blog

It is all about coding!

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>.

e.g,

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.

Advertisements

October 12, 2010 - Posted by | C#.NET

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: