Removing Order from NHibernate Criteria Query

有些话、适合烂在心里 提交于 2019-12-07 17:43:34

问题


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"));
    }

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?


回答1:


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"));
        }
    }

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




回答2:


I'd do something like this:

private ICriteria BaseQuery {
    get {
        return NHibernateSesssionManager.GetSession().CreateCriteria<Person>();
    }
}

public ICriteria StandardQuery {
    get {
        return BaseQuery.AddOrder("OrderProperty", Order.Desc);
    }
}

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


来源:https://stackoverflow.com/questions/1751422/removing-order-from-nhibernate-criteria-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!