Error on HQL Query distinct count of child collections (in elements error)
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 开发者_如何学Cam being clear in my question. Thank you in advance for your time.
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
精彩评论