使用聚合的分页投影nhibernate查询的总结果

时间:2011-10-20 08:00:39

标签: c# sql-server nhibernate

我正在动态构建一个需要实现分页的nhibernate预测查询。有点像...

var projections = Projections.ProjectionList();
foreach (var p in projection.Projections)
{
    IProjection newProjection = null;
    switch (p.AggregateFunc)
    {
        case AggregateFuncTypeEnum.GroupProperty:
            newProjection = Projections.GroupProperty(p.Path);
            break;
        case AggregateFuncTypeEnum.Sum:
            newProjection = Projections.Sum(p.Path);
            break;
        default:
            newProjection = Projections.Property(p.Path);
            break;
    }
    projections.Add(newProjection, p.Name);
}
criteria.SetProjection(projections).SetResultTransformer(new AliasToBeanResultTransformer(projectionType));

我可以得到前15个结果

criteria.SetFirstResult(0);
criteria.SetMaxResults(15);
var results = criteria.List();

但是我还需要发送另一个查询来获取记录总数,但到目前为止我还没有想到这一点。仍然需要应用投影,即如果结果按“代码”分组,并且总和为“成本”,那么100条记录可能会返回20行,而这就是我感兴趣的20条。

如何获取将返回的记录总数?感谢

1 个答案:

答案 0 :(得分:0)

也许这个:

var rowcount = CriteriaTransformer.Clone(criteria);

var goupprojections = Projections.ProjectionList();
var projections = Projections.ProjectionList();
foreach (var p in projection.Projections)
{
    IProjection newProjection = null;
    switch (p.AggregateFunc)
    {
        case AggregateFuncTypeEnum.GroupProperty:
            newProjection = Projections.GroupProperty(p.Path);
            goupprojections.Add(Projections.GroupProperty(p.Path), p.Name);
            break;
        case AggregateFuncTypeEnum.Sum:
            newProjection = Projections.Sum(p.Path);
            break;
        default:
            newProjection = Projections.Property(p.Path);
            break;
    }
    projections.Add(newProjection, p.Name);
}
criteria.SetProjection(projections).SetResultTransformer(new AliasToBeanResultTransformer(projectionType));

if (goupprojections.Aliases.Length == 0)
{
    rowcount.SetProjection(Projections.RowCount())
}
else
{
    rowcount.SetProjection(Projections.Count(goupprojections))
}

var results = criteria.Future();
var count = rowcount.FutureValue<int>();