The query results cannot be enumerated more than once?

前端 未结 4 1995
广开言路
广开言路 2020-12-15 22:49

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

4条回答
  •  一整个雨季
    2020-12-15 23:45

    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)

提交回复
热议问题