Thursday 15 October 2015

Sitecore indexed results and paging

In the past I often used a query like this to bring back paged results from a query on a Sitecore index:
query.Skip(page*pageSize).Take(pageSize);
However, after some research I realized that this is inefficient and actually causes some issues because the query is performed in memory.

N.B. Sitecore uses a reduced set of Linq operators, be careful when refactoring Sitecore Linq statements as tools like reSharper will break Sitecore Linq statements

Instead the Sitecore suggested approach to do this is to use the Page extension method, that is part of the QueryableExtension class in Sitecore.Contentsearch.Linq.

The above would become:
query.Page(page, pageSize);
Much neater!

1 comment: