Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The query results cannot be enumerated more than once?

I'm using LINQ to SQL to get a search result of a FullTextSearch stored procedure in Sql server 2008. I dragged the procedure from the server explorer to the designer, and got the method created with the appropriate return type and parameters. Now the problem is, I need to get the Count of the result of calling this method, so using my repository method (which will call the Sproc method and return the result as IQueryable) I make the following call.

var result = repository.FullTextSearch(searchText);
        int resultsCount = result.Count();
        var ret = result.Skip((pageNumber - 1) * PageSize).Take(PageSize).ToList();

This code generates an InvalidOperationException each time I try to run it, the exception says (yeah, you guessed it!) "The query results cannot be enumerated more than once."

The method that was generated for the Sproc returns ISingleResult which should be O.K. AFAIK. I need to support paging on my view, so I need to know the total number of pages, which (AFAIK again) is only possible if I could get the count of all items.

What am I missing here, guys?

like image 210
Galilyou Avatar asked Sep 01 '09 08:09

Galilyou


4 Answers

What you can do is add a ToList() call after repository.FullTextSearch(searchText). This way, the results are retrieved from the server, after which you can do with them whatever you want (since they are now loaded in-memory).

What you are trying to do now is run the same SQL query twice, which is rather inefficient.

like image 117
Ronald Wildenberg Avatar answered Oct 18 '22 00:10

Ronald Wildenberg


Since this is executing a stored procedure, all your lovely Skip / Take is largely redundant anyway... it has no choice but to bring all the data back (stored procedure calls are non-composable). The only thing it can do is not materialize objects for some of them.

I wonder if the better approach would be to refactor the code to make two calls:

int result = repository.FullTextSearchCount(searchText);
var result = repository.FullTextSearch(searchText, skip, take); // or similar

i.e. make the paging parameters part of the SPROC (and to the filtering at the database, using ROW_NUMBER() / OVER(...), or table-variables, temp-tables, etc) - or alternatively something similar with an OUTPUT parameter in the sproc:

int? count = null;
var result = repository.FullTextSearch(searchText, skip, take, ref count);

(I seem to recall that OUTPUT becomes ref, since TSQL OUTPUT is really input+output)

like image 22
Marc Gravell Avatar answered Oct 17 '22 23:10

Marc Gravell


Using ToList() can help to avoid this problem.

var result = repository.FullTextSearch(searchText).ToList();
like image 5
Sasha Avatar answered Oct 18 '22 00:10

Sasha


I would suggest that if you need the count, execute the result first. and then run the count from the list itself, as you don't use resultsCount in your result execution.

var result = repository.FullTextSearch(searchText);
var ret = result.Skip((pageNumber - 1) * PageSize).Take(PageSize).ToList();
int resultsCount = ret.Count();
like image 1
Kamal Avatar answered Oct 17 '22 23:10

Kamal