Limiting and paginating query results

When you execute a query, you can limit the number of results in the following ways:

  • You can retrieve only the first N results, where N is a number you specify.
  • You can retrieve N results, starting from the Mth result, where M and N are numbers you specify.
  • You can retrieve all results, starting from the Nth result, where N is a number you specify.

To achieve this, use the SetResultFilter (ASP.NET) or setResultFilter (JSP) method in the Query class. This method takes an object that implements the ResultFilter interface. There are two classes that implement this interface:

LimitFilter
Use this filter to retrieve only the first N results. The constructor for this class takes one integer as parameter.
PagingFilter

Use this filter, which takes two integers as parameters, for the following scenarios:

  • to retrieve N results, starting from the Mth result: set the first parameter to M - 1 (the results are zero-based), and the second parameter to N (the number of results you want to retrieve).
  • to retrieve all results, starting rom the Mth result: set the first parameter to M - 1, and the second parameter to -1.

For example, the following Paging Filter retrieves the 6th, 7th and 8th results (JSP example):

PagingFilter filter = new PagingFilter(5, 3)

The following Paging Filter retrieves all results, starting from the 6th result (JSP example):

PagingFilter filter = new PagingFilter(5, -1)

You can execute queries repeatedly, using the PagingFilter object to paginate your results into fixed sets of results.