What is the best way to compare an Int entry from a table in a case statement?
Using SQL server 2008 R2, Visual Basic Express with LINQ to SQL.
The code I tried doesnt work:
Private Sub UpdateSetOpt()
Dim db = New ACEDataContext
Dim SRM = From q In db.Settings
Where q.SettingID = frmMain.CurrentSID
Select q.RollMethod
Select Case SRM
Case 1
rbStandard.Checked = True
Case 2
rbProfession.Checked = True
Case 3
rbSpecies.Checked = True
Case 4
rbRandom.Checked = True
Case Else
rbStandard.Checked = False
rbProfession.Checked = False
rbSpecies.Checked = False
rbRandom.Checked = False
End Select
End Sub
SRMisn’t anIntegersince yourFromquery returns a collection of items. To get just the first, useSingle():If the query actually returns more than a single value the above code will fail; you need to use
Firstinstead ofSinglethen. Both will fail if no value is returned by the query. In that case,FirstOrDefaultmay be used instead (but probably isn’t appropriate in your situation).Adding to that, your
Select Caseis a sign of code smell. You should rather create an array of all the check boxes and use the integer to map into it: