【发布时间】:2011-07-03 10:32:15
【问题描述】:
如何使用 Castle ActiveRecords 和 LINQ 或 HQL 执行以下查询?
选择 a.id、s.classes、COUNT(p.id)、MAX(p.date) 作为最后一个、MIN(p.date) 作为第一个
来自帐户 a
左加入学校 s.account_id = a.id
左加入用户 u ON u.account_id = a.id
LEFT JOIN 点 p ON p.user_id = u.id
WHERE 付款 = "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 operation is not implemented-Exception:
NHibernate.Linq.Visitors.QueryModelVisitor.VisitGroupJoinClause(GroupJoinClause groupJoinClause, QueryModel queryModel, Int32 index)
【问题讨论】:
-
您是否必须按
s.classes分组才能获得有效的 SQL 查询? -
不,
s.classes只是学校表的一个 int 列,表示学校的班级数量。实际上,我需要 account 和 school 表中的更多列,我在此处省略了这些列以解决主要问题,但是发布的 SQL 查询在直接针对 de DB 执行时有效。
标签: c# sql linq hql castle-activerecord