2

I'm trying to retrieve a specific number of data for paging, I have no idea why the Skip Query returns an empty List and it throws the following exception "The result of a query cannot be enumerated more than once"

IEnumerable<ImgOrgSet> query = _ImgOrgRepository.GetImgOrgList();
IEnumerable<ImgOrgSet> queryPaginated = query.OrderBy(x => x.Id).Skip((CurrentPage - 1) * PageSize).Take(PageSize);

Debuging details

GSDa
  • 193
  • 2
  • 4
  • 21
  • possible duplicate of [The result of a query cannot be enumerated more than once](http://stackoverflow.com/questions/5723555/the-result-of-a-query-cannot-be-enumerated-more-than-once) – Spikolynn May 26 '14 at 19:57

2 Answers2

1

As suggested above in comments and the signposted duplicate you need to add .ToList() to make the results explicitly available for further processing.

Peter Smith
  • 5,528
  • 8
  • 51
  • 77
0

I think it throws the exception because you expanded the 'query' variable before in the 'debug window'. Try running it again and don't expand the query/debug.

Spikolynn
  • 4,067
  • 2
  • 37
  • 44
  • I don't think so, I expanded it right after the exception was thrown. I tried running it again without breakpoints and I got a null list. same problem. – GSDa May 26 '14 at 19:31
  • OK. Can you explain why you call GetImgOrgList with size/position arguments, then in second row you try to do it again with Skip((CurrentPage - 1) * PageSize).Take(PageSize) – Spikolynn May 26 '14 at 19:35
  • No, I only forgot to remove the arguments sorry (edited*), in the first row I retrieve all ImgOrg elements in DB, in the second row I try to get PageSize elements from the CurrentPage. – GSDa May 26 '14 at 19:44
  • 1
    http://stackoverflow.com/questions/5723555/the-result-of-a-query-cannot-be-enumerated-more-than-once suggests to add .ToList() to 'materialize' the query – Spikolynn May 26 '14 at 19:57