HQL 查询子集合的不同计数错误(元素错误)
首先,请原谅我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个满足您要求的 HQL 查询:
我将让您替换关联的真实名称。
您甚至可以通过避免课程中的连接来使其更短(并且更类似于您的 SQL 查询):
Here is a HQL query that does what you want:
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: