本文介绍了从NHibernate条件查询中删除订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个条件查询,用于显示结果页面.我还需要获取所有项目的总数.而不是有两个查询,一个用于分页结果,一个用于计数(因为除了.AddOrder()之外,它们是相同的

I have a criteria query that I am using to show pages of results. I also need to obtain the total count of all items. Rather than have two queries, one for paging the results and one for the count (since they are identical apart from the .AddOrder()

public ICriteria StandardQuery {
    get {
        return NHibernateSesssionManager.GetSession.CreateCriteria<Person>.AddOrder("OrderProperty", Order.Desc);
    }

public ICriteria CountQuery {
    get{
        return StandardQuery.SetProjection(Projections.Count("ID"));
    }

很显然,带有列"dbo.Person.ordercolumn"的CountQuery barfs在ORDER BY子句中无效,因为它既不包含在聚合函数中也不包含在GROUP BY子句中."

Obviously the CountQuery barfs with "Column "dbo.Person.ordercolumn" is invalid in the ORDER BY clause because it is not contained in either an aggregate function or the GROUP BY clause."

这很有意义,所以基本上我想做这样的事情.

This makes sense, so basically I want to do something like this.

public ICriteria CountQuery {
    get{
        return StandardQuery.RemoveOrders().SetProjection(Projections.Count("ID"));
    }

有没有办法做这样的事情?这样我就省去了具有两个重复查询的风险",一个用于分页,一个用于计数.显然,对任何一个查询的任何更改都需要在另一个上进行镜像,这是我不喜欢的风险.你会怎么做?

Is there a way to do something like this? So that I am saved the "risk" of having two duplicate queries, one for paging and one for count. Clearly any change to either query needs to be mirrored on the other and this is risk that I do not like. What would you do?

推荐答案

有一种专门用于此目的的方法.不幸的是,它使用起来有点混乱.

There's a method exactly for this. Unfortunately its a bit messy to use.

    private ICriteria NewCount
    {
        get
        {
            ICriteria countQuery = (ICriteria) StandardQuery.Clone();
            countQuery.ClearOrders();
            return countQuery.SetProjection(Projections.Count("ID"));
        }
    }

不知道为什么ClearOrders()会返回void而不是ICriteria,但是它可以工作!

No idea why ClearOrders() returns void instead of ICriteria, but it works!

这篇关于从NHibernate条件查询中删除订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 21:49