JoinQueryOver 中的 NHibernate QueryOver Sum
尽管我仔细阅读了 NHibernate Cookbook 和所有可用的论坛帖子,但我仍然无法完成这个简单的查询:
我的用户中每个人都有一个帐户。每个账户都有余额。 这些类看起来像这样:
public class User
{
public virtual int Id { get; set; }
public virtual Account Account { get; set; }
public virtual bool Active { get; set; }
}
public class Account
{
public virtual int Id { get; set; }
public virtual double Balance { get; set; }
}
现在我想对所有活跃用户的余额进行求和。而已... 在普通 SQL 中,这非常简单:
SELECT SUM(a.Balance)
FROM User u
INNER JOIN Account a
ON u.Account_id = a.Id
WHERE u.Active = 'true'
我不知道如何使用 NHibernate 3 中的新 QueryOver-Api 来解决这个问题。您能提供一个代码示例吗?
先感谢您!
丹尼尔·朗
编辑
我知道,使用 NHibernate Linq 也很容易,但我想使用 QueryOver 来解决它...这是有效的 Linq 示例:
var result = Session.Query<User>()
.Where(x => x.Active)
.Sum(x => x.Account.Balance)
解决方案
感谢 AlexCuse 我可以找到最终的解决方案(他非常非常接近) - 这是完整的代码:
User userAlias = null;
Account accountAlias = null;
session.QueryOver<User>(() => userAlias)
.JoinAlias(() => userAlias.Account, () => accountAlias)
.Where(() => userAlias.Active)
.Select(Projections.Sum<Account>(acct => accountAlias.Balance))
.SingleOrDefault<double>()
Though I was reading through the NHibernate Cookbook and all available forum-posts up and down, I'm still not able to get this simple query done:
I have users with everyone having one account. Each account hast a balance.
The classes look like that:
public class User
{
public virtual int Id { get; set; }
public virtual Account Account { get; set; }
public virtual bool Active { get; set; }
}
public class Account
{
public virtual int Id { get; set; }
public virtual double Balance { get; set; }
}
Now I would like to sum the balance of all active users. Nothing more...
In plain SQL it is quite easy:
SELECT SUM(a.Balance)
FROM User u
INNER JOIN Account a
ON u.Account_id = a.Id
WHERE u.Active = 'true'
I don't have any I idea, how I could solve that with the new QueryOver-Api from NHibernate 3. Could you please provide a code-example?
Thank you in advance!
Daniel Lang
EDIT
I know, that with NHibernate Linq it is very easy too, but I would like to solve it using QueryOver... Here is the working Linq-Example:
var result = Session.Query<User>()
.Where(x => x.Active)
.Sum(x => x.Account.Balance)
SOLUTION
Thanks to AlexCuse I could find the final solution (he was very very close) - here is the full code:
User userAlias = null;
Account accountAlias = null;
session.QueryOver<User>(() => userAlias)
.JoinAlias(() => userAlias.Account, () => accountAlias)
.Where(() => userAlias.Active)
.Select(Projections.Sum<Account>(acct => accountAlias.Balance))
.SingleOrDefault<double>()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你尝试过这样的事情吗?
我不确定 QueryOver API 中的 UniqueResult 等效项是什么,因此必须仔细检查基本标准。
Have you tried something like this?
I'm not sure what the UniqueResult equivalent is in the QueryOver API, so had to go through the underlying criteria.
您在答案中写道:
您不需要通用类型的别名。它可能是:
You wrote in your answer:
You don't need to have an alias for the generic type. It could be: