将 Case 语句与表中的 Int 条目结合使用

发布于 2024-11-01 06:00:08 字数 763 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

翻身的咸鱼 2024-11-08 06:00:08

SRM 不是 Integer,因为您的 From 查询返回项目集合。要仅获取第一个值,请使用 Single()

Dim SRM = (From q In db.Settings
           Where q.SettingID = frmMain.CurrentSID
           Select q.RollMethod).Single()

如果查询实际上返回多个单个值,则上述代码将失败;然后您需要使用 First 而不是 Single 。如果查询没有返回值,则两者都会失败。在这种情况下,可以改用 FirstOrDefault (但可能不适合您的情况)。

除此之外,您的 Select Case 是代码异味的标志。您应该创建一个包含所有复选框的数组,并使用整数映射到其中:

Dim checks As CheckBox() = New CheckBox() { _
    rbStandard, rbProfession, rbSpecies, rbRandom }

' Unset all checkboxes:
For Each check In checks
    check.Checked = False
End For

If SRM > 0 AndAlso SRM <= checks.Length Then
    checks(SRM - 1).Checked = True
End If

SRM isn’t an Integer since your From query returns a collection of items. To get just the first, use Single():

Dim SRM = (From q In db.Settings
           Where q.SettingID = frmMain.CurrentSID
           Select q.RollMethod).Single()

If the query actually returns more than a single value the above code will fail; you need to use First instead of Single 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:

Dim checks As CheckBox() = New CheckBox() { _
    rbStandard, rbProfession, rbSpecies, rbRandom }

' Unset all checkboxes:
For Each check In checks
    check.Checked = False
End For

If SRM > 0 AndAlso SRM <= checks.Length Then
    checks(SRM - 1).Checked = True
End If
独享拥抱 2024-11-08 06:00:08

在您的情况下,SRM 不是 int,而是 IEnumerabe

您想要第一个元素:

SELECT CASE SRM.FirstOrDefault():

SRM in your case is not an int but IEnumerabe<int>.

You want the first element:

SELECT CASE SRM.FirstOrDefault():
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文