我有一个代码来查询/分页ProductPrice列表...我的ProductPrice对象有一个产品...... 代码工作得很好......
但是看看log4net我有2个SELECT发生... 是对的吗?
我的代码:
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
.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>();
编辑
ProductPrice:
public class ProductPrice : Entity
{
public virtual string Sku { get; set; }
public virtual decimal Price { get; set; }
public virtual Product Product { get; set; }
...
}
产品:
public class ProductPrice : Entity
{
public virtual string Name { get; set; }
public virtual IList<ProductPrice> Prices { get; set; }
...
}
映射由Fluent NHibernate生成......
由于
答案 0 :(得分:3)
您过早地执行“ .Value ”以获取行数。你应该保持这样:
var rowCountQuery = query.ToRowCountQuery();
var rowCount = rowCountQuery.FutureValue<int>();
这样查询就不会真正执行,只是推迟了。
在主查询(看起来没问题)之后,您现在可以真正获取行计数整数,并且两个查询应该同时发送到数据库:
totalCount = rowCount.Value;