Removing order from NHibernate level request

I have a criteria query that I use to display result pages. I also need to get the total number of all items. Instead of two queries, one for paging results and one for counting (since they are identical except for .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, barfs CountQuery with the "Column" dbo.Person.ordercolumn "is not valid in the ORDER BY clause because it is not contained in either the 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"));
    }

- ? "" , . , , , . ?

+3
2

. , .

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

, ClearOrders() void ICriteria, !

+6

- :

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

Source: https://habr.com/ru/post/1723151/


All Articles