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

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

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)

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.