与骨料分页预计NHibernate的查询结果总 [英] Total results of paged projected nhibernate query with aggregates

查看:128
本文介绍了与骨料分页预计NHibernate的查询结果总的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态地构建一个NHibernate的预测查询需要实现分页。喜欢的东西...

I'm dynamically building a nhibernate projected query that needs to implement paging. Something like...

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个结果,像这样

I can get the first 15 results like so

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



但我也需要发送另一个查询得到的记录总数,但到目前为止我没能想出解决办法。投影仍需如果结果由'代码'与'成本',那么100个记录的总和分组可能会返回20行进行,即应用,它是20,我感兴趣的东西。

But I also need to send another query to get the total number of records but so far I've failed to figure this out. The projection still needs to be applied i.e. if the results are grouped by 'code' with a sum of 'cost' then 100 records might return 20 rows, and it's the 20 I'm interested in.

我如何获得的记录总数将返回?谢谢

How do I get the total number of records that will be returned? Thanks

推荐答案

也许这样的:

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>();

这篇关于与骨料分页预计NHibernate的查询结果总的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆