HQL 查询子集合的不同计数错误(元素错误)

发布于 2024-12-09 06:34:08 字数 1133 浏览 0 评论 0原文

首先,请原谅我的 NHibernate 词汇量有限,所以我可能会说一些错误的东西......这是我的问题:

我正在寻找的结果是一门课程的不同学生的计数。我有三门课: 课程、学生、课程日期。 Courses 包含与 CourseDates 的 HasMany 关系。 CourseDates 是每节课发生的日期的集合,并且包含与 Students 类的 HasAndBelongsToMany 关系。

我需要做的是从课程发生的所有日期中获取学生的明确计数。下面是我想要复制的 SQL 语句的示例。结果是一个数字(长整型)。此特定示例生成结果: 5

SELECT COUNT(DISTINCT dbo.Literacy_Course_DatesStudents.IDStudent) AS StudentCount FROM Literacy_Course_DatesStudents INNER JOIN Literacy_Course_Dates ON Literacy_Course_DatesStudents.IDDate = Literacy_Course_Dates.IDDate WHERE (Literacy_Course_Dates.IDCourse = 28)

下面是从我专门为此报告创建的新类中编写的查询...但我不断收到错误: 类型异常抛出“Antlr.Runtime.MissingTokenException”。通常我认为当我没有将 CourseEnrolledCount 类导入到其他类中时会引发此错误,但我已经这样做了。

Dim q As Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)
q = New Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)(GetType(Literacy.Courses.CourseDates), "select new CourseEnrolledCount(Count(Distinct o.IDStudent in elements(t.Students) as o)) from CourseDates t Where t.Course.IDCourse = :courseid")

如果我需要提供更多信息,请告诉我。我希望我的问题很清楚。预先感谢您抽出时间。

First of all, please forgive me for my vocabulary is a little limited with NHibernate so I might call something the wrong thing...here is my question:

Result I am looking for is a count of distinct students for a course. I have three classes:
Courses, Students, CourseDates.
Courses contains a HasMany relationship with CourseDates.
CourseDates is a collection of dates on which each class has occurred and contains a HasAndBelongsToMany relationship with the Students class.

What I need to do is a get a distinct count of the Students from all the dates a course has occurred. Here is an example of a SQL statement that I want to replicate. The result is a number (long). This specific example produces the result: 5

SELECT COUNT(DISTINCT dbo.Literacy_Course_DatesStudents.IDStudent) AS StudentCount FROM
Literacy_Course_DatesStudents INNER JOIN Literacy_Course_Dates ON Literacy_Course_DatesStudents.IDDate = Literacy_Course_Dates.IDDate WHERE (Literacy_Course_Dates.IDCourse = 28)

Below is the query written from within a new class that I created specifically for this report...but I keep getting an error: Exception of type 'Antlr.Runtime.MissingTokenException' was thrown. Usually I thought this error was thrown when I didn't have CourseEnrolledCount class imported into the other classes but I have done that.

Dim q As Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)
q = New Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)(GetType(Literacy.Courses.CourseDates), "select new CourseEnrolledCount(Count(Distinct o.IDStudent in elements(t.Students) as o)) from CourseDates t Where t.Course.IDCourse = :courseid")

Let me know if I need to provide additional information. I hope I am being clear in my question. Thank you in advance for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浊酒尽余欢 2024-12-16 06:34:08

这是一个满足您要求的 HQL 查询:

select count(disctinct student.id) from Course course
inner join course.courseDates courseDate
inner join courseDate.students student
where course.id = :courseId

我将让您替换关联的真实名称。

您甚至可以通过避免课程中的连接来使其更短(并且更类似于您的 SQL 查询):

select count(disctinct student.id) from CourseDate courseDate
inner join courseDate.students student
where courseDate.course.id = :courseId

Here is a HQL query that does what you want:

select count(disctinct student.id) from Course course
inner join course.courseDates courseDate
inner join courseDate.students student
where course.id = :courseId

I'll let you substitute the real names of the associations.

You could even make it shorter (and more similar to your SQL query) by avoiding the join on the course:

select count(disctinct student.id) from CourseDate courseDate
inner join courseDate.students student
where courseDate.course.id = :courseId
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文