如何使用 LINQ 或 HQL 执行以下 SQL 查询

发布于 2024-11-18 06:31:20 字数 1248 浏览 4 评论 0原文

如何使用 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 分组

这些表按以下方式关联: ER-Diagramm

我还为所有定义了正确关系的表提供了 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:
ER-Diagramm

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 技术交流群。

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

发布评论

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

评论(2

猥︴琐丶欲为 2024-11-25 06:31:20

使用 HQL 找到了解决方案 - 我仍然愿意使用 LINQ 解决方案:

HqlBasedQuery query = new HqlBasedQuery(typeof(AccountRecord),
    "SELECT a, s, COUNT(p), MIN(p.DateUTC), MAX(p.DateUTC) " +
    "FROM AccountRecord a " +
    "LEFT JOIN a.Schools s " +
    "LEFT JOIN a.Users u " +
    "LEFT JOIN u.Points p " +
    "WHERE a.PaymentType=:payment GROUP BY a.Id");
query.SetParameter("payment", "S");
var result = from object[] row in (ArrayList)ActiveRecordMediator.ExecuteQuery(query)
                select new
                {
                    Account = row[0] as AccountRecord,
                    School = row[1] as SchoolRecord,
                    Count = row[2],
                    First = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[3])),
                    Last = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[4]))
                };

Found the solution using HQL - I'm still open for a LINQ solution:

HqlBasedQuery query = new HqlBasedQuery(typeof(AccountRecord),
    "SELECT a, s, COUNT(p), MIN(p.DateUTC), MAX(p.DateUTC) " +
    "FROM AccountRecord a " +
    "LEFT JOIN a.Schools s " +
    "LEFT JOIN a.Users u " +
    "LEFT JOIN u.Points p " +
    "WHERE a.PaymentType=:payment GROUP BY a.Id");
query.SetParameter("payment", "S");
var result = from object[] row in (ArrayList)ActiveRecordMediator.ExecuteQuery(query)
                select new
                {
                    Account = row[0] as AccountRecord,
                    School = row[1] as SchoolRecord,
                    Count = row[2],
                    First = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[3])),
                    Last = (new DateTime(1970, 1, 1, 0, 0, 0, 0)).AddSeconds(Convert.ToDouble(row[4]))
                };
瑾兮 2024-11-25 06:31:20

我认为您的 Linq 查询类似于:

var result = from a in AccountRecord.Queryable
             join s in SchoolRecord.Queryable on a.id equals s.account_id
             join u in UserRecord.Queryable   on a.id equals u.account_id
             join p in PointsRecord.Queryable on u.id equals p.user_id
             where a.payment == "S"
             group by a.id
             select new
             {
               Account = a,
               School = s,
               Count = p.Count()
             };

尽管我不确定 NHibernate 处理 group byCount() 组合的效果如何。您可能想看看生成的 SQL 最终会是什么(如果它没有抛出错误)。

如果这不起作用,您可能需要选择回记录,然后在应用程序中对它们进行分组/计数,更像是:

var data = from a in AccountRecord.Queryable
           join s in SchoolRecord.Queryable on a.id equals s.account_id
           join u in UserRecord.Queryable   on a.id equals u.account_id
           join p in PointsRecord.Queryable on u.id equals p.user_id
           where a.payment == "S"
           select new
           {
             Account = a,
             School = s,
             Count = c
           };
var grouped = data.ToList.GroupBy(x => x.Account.Id);

I think your Linq query would be something like:

var result = from a in AccountRecord.Queryable
             join s in SchoolRecord.Queryable on a.id equals s.account_id
             join u in UserRecord.Queryable   on a.id equals u.account_id
             join p in PointsRecord.Queryable on u.id equals p.user_id
             where a.payment == "S"
             group by a.id
             select new
             {
               Account = a,
               School = s,
               Count = p.Count()
             };

Though I'm not sure how well NHibernate will handle the group by and Count() 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:

var data = from a in AccountRecord.Queryable
           join s in SchoolRecord.Queryable on a.id equals s.account_id
           join u in UserRecord.Queryable   on a.id equals u.account_id
           join p in PointsRecord.Queryable on u.id equals p.user_id
           where a.payment == "S"
           select new
           {
             Account = a,
             School = s,
             Count = c
           };
var grouped = data.ToList.GroupBy(x => x.Account.Id);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文