score:1

Accepted answer

When I tried running it locally I got this exception

System.Exception: Grouping over multiple tables is not supported yet

I am not sure why you would be getting the less pretty version of that error. But if you pull the GroupBy out of the query, things seem to work fine.

query {
    for student in db.Student do
    join course in db.CourseSelection
        on (student.StudentID = course.StudentID)
    select (student.Name, course.Id)
} 
|> Seq.countBy snd
|> Seq.iter (fun (student, classCount) -> printfn "Student %s has %i classes" student classCount)

score:0

I believe that the reason why your first query is not working is because of the groupBy student into group.

I think what you actually want to write is groupBy student.StudentID into group.

score:0

I found an answer from a similar question:

query {
    for student in db.Student do

    join course in db.CourseSelection
        on (student.StudentID = course.StudentID)

    let count = query { 
        for c in db.CourseSelection do
        where (c.StudentID = student.StudentID)
        select c
        count
    }


    select (student.Name, count)
    distinct

}

|> Seq.iter (fun (student, classCount) -> printfn "Student %s has %i classes" student classCount)

The underlying paradigm of these query expressions still eludes me.

EDIT:

I found that you can join implicitly without needing to use the join operator

query {
    for student in db.Student do
    let count = query {
        for course in student.CourseSelection do
        count    
    }

    select (student.Name, count)
}
|> Seq.iter (fun (name, count) -> printfn "%s has %i courses" name count)

Related Articles