Tutorial :L2S, Caching, and error: The query results cannot be enumerated more than once



Question:

I have a fairly complex query (that includes a table valued function to allow full text searching) that I am trying to cache (HttpRuntime.Cache) for paging purposes. When I try to use the cached L2S query, I get the error stated above: The query results cannot be enumerated more than once.

I have tried assigning my query to another IQueryable object by calling AsIQueryable() on the cached object, but that does not help.

Any ideas?


Solution:1

You could store the results of the query in the cache instead of the query itself by calling .ToArray() or .ToList() extension methods which will execute the query immediately. Then you can enumerate the results from the cache as much as you wish.


Solution:2

use

var retVal = (.....).First() or ToList();

and use retVal.Name, retVal.Surname ....

if you use ToList();, you need to give an index like retVal[1].Name


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »