Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to write a linq statement that will change the letter of a grade into a number so i can calculate gpa.
Can anyone tell me what is wrong with the below code sinnpet.

How and where can I declare the word grade so i can stop getting the error grade is not declared?


VB
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim Gpa As String = Aggregate CreditHours In CoursesDataSet.tblCourses
                           Let gradeValue = If(Grade = "A", 4, If(Grade = "B", 3, If(Grade = "C", 2, If(Grade = "D", 1, 0))))
                           Select CreditHours * gradeValue
                           Into Sum()



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


    End Sub
Posted
Updated 5-May-15 9:36am
v2
Comments
Maciej Los 5-May-15 14:18pm    
http://stackoverflow.com/questions/30034330/linq-statement-error/30035184

 
Share this answer
 
Hi,
you could use the ASCII-Code of the Character.
For Example :
VB
dim myInt as integer = ASC(Grade)

In this Example myInt will have 65 for "A", 66 for "B" and so on.
But Attention : the ASCII-Code for Uppercase and Lowercase is not the same !

Greetings
Ralf
 
Share this answer
 
Comments
karodhill 7-May-15 11:06am    
i was thinking of a more simpler where clause i tried to do it this way

but 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

<pre>
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()</pre>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900