JoinQueryOver 中的 NHibernate QueryOver Sum

发布于 2024-11-15 21:58:45 字数 1385 浏览 7 评论 0原文

尽管我仔细阅读了 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 技术交流群。

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

发布评论

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

评论(2

画骨成沙 2024-11-22 21:58:45

你尝试过这样的事情吗?

session.QueryOver<User>(() => userAlias)
    .JoinAlias(() => userAlias.Account, () => accountAlias)
    .Where(() => userAlias.Active)
    .Select(Projections.Sum<Account>(acct => acct.Balance))
    .UnderlyingCriteria.UniqueResult()

我不确定 QueryOver API 中的 UniqueResult 等效项是什么,因此必须仔细检查基本标准。

Have you tried something like this?

session.QueryOver<User>(() => userAlias)
    .JoinAlias(() => userAlias.Account, () => accountAlias)
    .Where(() => userAlias.Active)
    .Select(Projections.Sum<Account>(acct => acct.Balance))
    .UnderlyingCriteria.UniqueResult()

I'm not sure what the UniqueResult equivalent is in the QueryOver API, so had to go through the underlying criteria.

萌能量女王 2024-11-22 21:58:45

您在答案中写道:

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>()

您不需要通用类型的别名。它可能是:

Account accountAlias = null;

session.QueryOver<User>()
    .JoinAlias(user => user.Account, () => accountAlias)
    .Where(user => user.Active)
    .Select(Projections.Sum<Account>(acct => accountAlias.Balance))
    .SingleOrDefault<double>() 

You wrote in your answer:

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>()

You don't need to have an alias for the generic type. It could be:

Account accountAlias = null;

session.QueryOver<User>()
    .JoinAlias(user => user.Account, () => accountAlias)
    .Where(user => user.Active)
    .Select(Projections.Sum<Account>(acct => accountAlias.Balance))
    .SingleOrDefault<double>() 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文