0

I have a linq statement where i turned letter grades into numbers to calculate a gpa can anyone tell me why no matter what grade i click i get the same gpa of 68 for each one? maybe someone can look over my code and give me an answer or hint thanks

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click


        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
        dic.Add("A", 4)
        dic.Add("B", 3)
        dic.Add("C", 2)
        dic.Add("D", 1)
        dic.Add("F", 0)

        Dim gpa = Aggregate Grade In CoursesDataSet.tblCourses.AsEnumerable()
           Select Grade.Field(Of Integer)("CreditHours") * dic(Grade.Field(Of String)("Grade"))
           Into Sum()


        MessageBox.Show("GPa:" & gpa.ToString("C2"),
                      "College Courses", MessageBoxButtons.OK,
MessageBoxIcon.Information)
    End Sub

End Class

Click Here

2
Contributors
3
Replies
18
Views
2 Years
Discussion Span
Last Post by djjeavons
1

Hi

Currently you are performing an aggregate (sum) function over the entire DataTable so you will always get a total sum for the table. If you want it to be for a specific grade then you will need to include some form of WHERE criteria. While I couldn't replicate your code exactly as you have it, the following is an example:

Dim gpa = Aggregate Grade In coursesDataSet.Tables("tblCourses").Select("Grade='B'").AsEnumerable()
   Select Grade.Field(Of Integer)("CreditHours") * dic(Grade.Field(Of String)("Grade"))
   Into Sum()

Note the Select statement to filter the Grade on 'B'. You should be able to do the same using the following syntax (but not tested):

Dim gpa = Aggregate Grade In CoursesDataSet.tblCourses.Select("Grade='B'").AsEnumerable()
           Select Grade.Field(Of Integer)("CreditHours") * dic(Grade.Field(Of String)("Grade"))
       Into Sum()

HTH

Edited by djjeavons

0

tried it this way with a where clause now i get an error
for the grades saying option strict on disallows implicit conversions from
string to boolean

  Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

        Dim dic As Dictionary(Of String, Integer) = New Dictionary(Of String, Integer)
        dic.Add("A", 4)
        dic.Add("B", 3)
        dic.Add("C", 2)
        dic.Add("D", 1)
        dic.Add("F", 0)



        Dim gpa = Aggregate Grade In CoursesDataSet.tblCourses.AsEnumerable()
           Where Grade.Grade = "A" Or "B" Or "C" Or "D" Or "F"
           Select Grade.Field(Of Integer)("CreditHours") * dic(Grade.Field(Of String)("Grade"))
           Into Sum()




        MessageBox.Show("GPa:" & gpa.ToString(CInt("C2")),
                       "College Courses", MessageBoxButtons.OK,
 MessageBoxIcon.Information)
0

Did you try my example that uses the Select method of the DataTable? That effectively limits the rows that will form the base data to be aggregated.

Also, why in your example are you using a where criteria for all grades. If that did work syntactically, surely you would still get the same result as it would select all grades. Actually, just re-read and you are using Or so probably not but still not correct for what I believe you want to achieve. Unless I have misunderstood.

Edited by djjeavons

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.