从 NHibernate Criteria 查询中删除订单
我有一个条件查询,用于显示结果页面。我还需要获得所有项目的总数。而不是有两个查询,一个用于对结果进行分页,一个用于计数(因为除了 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一种方法专门用于此目的。不幸的是它使用起来有点混乱。
不知道为什么 ClearOrders() 返回 void 而不是 ICriteria,但它有效!
There's a method exactly for this. Unfortunately its a bit messy to use.
No idea why ClearOrders() returns void instead of ICriteria, but it works!
我会做这样的事情:
I'd do something like this: