NHibernate Future<T> analyse
I have a code to query/paginate a ProductPrice list... My ProductPrice Object has a Product... The code works fine...
But looking at log4net I have 2 SELECT happening... Is that right?
My code :
var query = Session.QueryOver<ProductPrice>();
Product product = null;
query.JoinQueryOver(mg => mg.Product, () => product);
query.WhereRestrictionOn(() => product.Name).IsLike("Asics", MatchMode.Anywhere)
.OrderBy(() => product.Name);
var rowCountQuery = query.ToRowCountQuery();
totalCount = rowCountQuery.FutureValue<int>().Value;
var firstResult = pageIndex * pageSize;
ProductViewModel productViewModel = null;
var productsViewModel = query
.SelectList(l => l
开发者_C百科 .Select(() => product.Id).WithAlias(() => productViewModel.Id)
.Select(() => product.Name).WithAlias(() => productViewModel.Name)
.Select(mg => mg.Price).WithAlias(() => productViewModel.Price))
.TransformUsing(Transformers.AliasToBean<ProductViewModel>())
.Skip(firstResult)
.Take(pageSize)
.Future<ProductViewModel>();
edited
ProductPrice:public class ProductPrice : Entity
{
public virtual string Sku { get; set; }
public virtual decimal Price { get; set; }
public virtual Product Product { get; set; }
...
}
Product:
public class ProductPrice : Entity
{
public virtual string Name { get; set; }
public virtual IList<ProductPrice> Prices { get; set; }
...
}
The mapping is generated by Fluent NHibernate...
Thanks
You're doing the ".Value" too soon to get the row count. You should keep it like:
var rowCountQuery = query.ToRowCountQuery();
var rowCount = rowCountQuery.FutureValue<int>();
This way the query is not really executed, just deferred.
After the main query, which seems ok, you may now really fetch the row count integer, and both queries should be sent at the same time to the database:
totalCount = rowCount.Value;
精彩评论