I have this logic in an extension method that creates a paging wrapper:
int total = 0;
if (query is IRavenQueryable<T>)
{
RavenQueryStatistics stats;
var rQuery = (IRavenQueryable<T>)query;
rQuery.Statistics(out stats);
total = stats.TotalResults;
}
else
{
total = query.Count();
}
My problem is that when I do a raven query, my stats.TotalResults is zero, unless I hover over rQuery during debug and try to expand the results.
What am I doing wrong here? How to I get raven to consistently give me the results I need?
Here is the whole method:
public static PagingModel<T> Page<T>(this IEnumerable<T> query, int page, int count)
{
if (page < 1)
throw new ArgumentOutOfRangeException("Page is one-based and must be greater than zero");
int total = 0;
if (query is IRavenQueryable<T>)
{
RavenQueryStatistics stats;
var rQuery = (IRavenQueryable<T>)query;
rQuery.Statistics(out stats);
total = stats.TotalResults;
}
else
{
total = query.Count();
}
var results = query
.Skip((page - 1) * count)
.Take(count)
.ToArray();
return new PagingModel<T>()
{
Page = page,
Rows = results,
Total = total /count
};
}
I figured it out myself. It turns out that RavenQueryStatistics sets the TotalResults by reference and the total is only available after the results have been resolved. (I originally thought it made a separate trip to the DB.)