如何使用 LINQ 或 HQL 执行以下 SQL 查询
如何使用 Castle ActiveRecords 和 LINQ 或 HQL 执行以下查询?
选择 a.id、s.classes、COUNT(p.id)、MAX(p.date) 作为最后一个、MIN(p.date) 作为第一个
来自帐户 a
左加入学校 s.account_id = a.id
LEFT JOIN 用户 u ON u.account_id = a.id
LEFT JOIN 点 p ON p.user_id = u.id
付款方式=“S”
按 a.id 分组
这些表按以下方式关联:
我还为所有定义了正确关系的表提供了 ActiveRecord 类(如果我按步骤进行查询,它可以工作) ,但速度很慢,因为有很多行),我尝试了以下方法,但没有成功:
var result = from account in AccountRecord.Queryable
join s in SchoolRecord.Queryable on account equals s.Account into schools
from school in schools.DefaultIfEmpty(null)
join user in UserRecord.Queryable on account equals user.Account
join p in PointsRecord.Queryable on user equals p.User into points
where account.PaymentType == "S"
select new { Account = account, School = school, Count = points.Count() };
抛出以下 The method or opera is not Implement
-Exception at:
NHibernate.Linq.Visitors.QueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 索引)
How can I execute the following query using Castle ActiveRecords and LINQ or HQL?
SELECT a.id, s.classes, COUNT(p.id), MAX(p.date) AS last, MIN(p.date) AS first
FROM account a
LEFT JOIN school s ON s.account_id = a.id
LEFT JOIN user u ON u.account_id = a.id
LEFT JOIN points p ON p.user_id = u.id
WHERE payment = "S"
GROUP BY a.id
The tables are related in the following way:
I also have ActiveRecord classes for all tables with the correct relations defined (if I do the query in steps it works, but it is slow as there are a lot of rows) and I tried the following which didn't worked:
var result = from account in AccountRecord.Queryable
join s in SchoolRecord.Queryable on account equals s.Account into schools
from school in schools.DefaultIfEmpty(null)
join user in UserRecord.Queryable on account equals user.Account
join p in PointsRecord.Queryable on user equals p.User into points
where account.PaymentType == "S"
select new { Account = account, School = school, Count = points.Count() };
which threw the following The method or operation is not implemented
-Exception at:
NHibernate.Linq.Visitors.QueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 index)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 HQL 找到了解决方案 - 我仍然愿意使用 LINQ 解决方案:
Found the solution using HQL - I'm still open for a LINQ solution:
我认为您的 Linq 查询类似于:
尽管我不确定 NHibernate 处理
group by
和Count()
组合的效果如何。您可能想看看生成的 SQL 最终会是什么(如果它没有抛出错误)。如果这不起作用,您可能需要选择回记录,然后在应用程序中对它们进行分组/计数,更像是:
I think your Linq query would be something like:
Though I'm not sure how well NHibernate will handle the
group by
andCount()
combination. You might want to see what the generated SQL ends up being, if it doesn't throw an error.If that doesn't work, you might want to select the records back, and group/count them in the application instead, more like: