How to use nhibernate ToFuture query with nhibernte linq
Asked Answered
F

2

8

Does ToFuture works with nhibernate linq? If so, how do you use it?

Farra answered 30/3, 2011 at 15:26 Comment(0)
M
3

Yes it does. Here's a simple example:

var blogs = _session.Query<Blog>()
    .Take(30)
    .ToFuture();
var blogCount= _session.Query<Blog>()
    .ToFutureValue(x => x.Count());

Console.WriteLine(blogCount.Value); // DB is queried here.

Here's an example of where I've used it for a customer search form that displayed paged search results and and a total count of search results. Notice you can reuse an IQueryable to create two futures. The Filter methods built an IQueryable based on what fields the user searched by.

int resultsPerPage = 50;
var query = _session.Query<CustomerSearch>()
    .FilterById(model)
    .FilterByFirstName(model)
    .FilterByLastName(model)
    .FilterBySocialSecurityNumber(model)    
    .FilterByPrimaryPhoneNumber(model);
var futureResults = query
    .OrderBy(x => x.Id)
    .Skip(model.Page * resultsPerPage)
    .Take(resultsPerPage)
    .ToFuture();
var futureCount = query.ToFutureValue(x => x.Count());
Mouldy answered 29/4, 2011 at 21:22 Comment(0)
H
5

Be carefull ToFuture only works if the database driver supports MulitpleQueries. This is only the case in some drivers (eg. MySql, SqlServer) but not in all (eg. Oracle)

Historian answered 2/12, 2011 at 10:53 Comment(0)
M
3

Yes it does. Here's a simple example:

var blogs = _session.Query<Blog>()
    .Take(30)
    .ToFuture();
var blogCount= _session.Query<Blog>()
    .ToFutureValue(x => x.Count());

Console.WriteLine(blogCount.Value); // DB is queried here.

Here's an example of where I've used it for a customer search form that displayed paged search results and and a total count of search results. Notice you can reuse an IQueryable to create two futures. The Filter methods built an IQueryable based on what fields the user searched by.

int resultsPerPage = 50;
var query = _session.Query<CustomerSearch>()
    .FilterById(model)
    .FilterByFirstName(model)
    .FilterByLastName(model)
    .FilterBySocialSecurityNumber(model)    
    .FilterByPrimaryPhoneNumber(model);
var futureResults = query
    .OrderBy(x => x.Id)
    .Skip(model.Page * resultsPerPage)
    .Take(resultsPerPage)
    .ToFuture();
var futureCount = query.ToFutureValue(x => x.Count());
Mouldy answered 29/4, 2011 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.