从 NHibernate Criteria 查询中删除订单

发布于 2024-08-11 23:57:20 字数 792 浏览 7 评论 0原文

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

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

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

显然,带有“Column”dbo.Person.ordercolumn”的 CountQuery barfs 在 ORDER BY 子句中是无效的,因为它不包含在聚合函数或 GROUP BY 子句中。”

这是有道理的,所以基本上我想做这样的事情。

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

有没有办法做这样的事情?这样我就可以避免“风险”有两个重复的查询,一个用于分页,一个用于计数,显然对任何一个查询的任何更改都需要反映在另一个查询上,这是我不喜欢的风险。

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扎心 2024-08-18 23:57:20

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

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

不知道为什么 ClearOrders() 返回 void 而不是 ICriteria,但它有效!

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!

丿*梦醉红颜 2024-08-18 23:57:20

我会做这样的事情:

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

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"));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文