将 Case 语句与表中的 Int 条目结合使用
在 case 语句中比较表中的 Int 条目的最佳方法是什么?
使用 SQL Server 2008 R2、Visual Basic Express 和 LINQ to SQL。
我尝试过的代码不起作用:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SRM
不是Integer
,因为您的From
查询返回项目集合。要仅获取第一个值,请使用Single()
:如果查询实际上返回多个单个值,则上述代码将失败;然后您需要使用
First
而不是Single
。如果查询没有返回值,则两者都会失败。在这种情况下,可以改用FirstOrDefault
(但可能不适合您的情况)。除此之外,您的
Select Case
是代码异味的标志。您应该创建一个包含所有复选框的数组,并使用整数映射到其中:SRM
isn’t anInteger
since yourFrom
query 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
First
instead ofSingle
then. Both will fail if no value is returned by the query. In that case,FirstOrDefault
may be used instead (but probably isn’t appropriate in your situation).Adding to that, your
Select Case
is a sign of code smell. You should rather create an array of all the check boxes and use the integer to map into it:在您的情况下,
SRM
不是 int,而是IEnumerabe
。您想要第一个元素:
SRM
in your case is not an int butIEnumerabe<int>
.You want the first element: