I'm interested on solution how to use Futures in counting something having Linq in mind. I'm not interested in Criteria api, just plain linq.
If I have:
IEnumerable<User> dbUsers = userquery.Future<User>();
IFutureValue<long> count = totalcountQuery.FutureValue<long>();
direct translation should be:
var dbUsers = userQuery.ToFuture();
var count = userQuery.LongCount();
Now I need to access count query to know exact number of populated records;
something like this, although this code wont compile:
totalRecords = (int)count.Value;
This is not directly supported by NHibernate, but it is very easy to implement (explained in this blog post). You need to write a new extension method.
public static IFutureValue<TResult> ToFutureValue<TSource, TResult>
(this IQueryable<TSource> source, Expression<Func<IQueryable<TSource>, TResult>> selector)
where TResult:struct where TSource: class
{
var provider = (INhQueryProvider) source.Provider;
var method = ((MethodCallExpression) selector.Body).Method;
var expression = Expression.Call(null, method, source.Expression);
return (IFutureValue<TResult>) provider.ExecuteFuture(expression);
}
Then it is easy to get a paged query
var query = session.Query<User>().Where(...);
var count = query.ToFutureValue(u => u.Count());
var results = query.OrderBy(u => u.FullName).Skip(n).Take(m).ToFuture()