如何在ASP.NET中选择多个情况

huangapple go评论53阅读模式
英文:

How to select multiple case in asp.net

问题

我正在进行一个项目,该项目使用Visual Basic编写ASP.NET代码。当我在ASP.NET中使用选择案例语句时,我有些困惑。我的ASP.NET连接到SQL Server数据库,我有一个名为“Person”的表,它有“Id”(decimal)、“Name”(string)和“Nationality”(string)字段。在该表中,有些人有2个或更多国籍,我为“Nationality”编写了选择案例,例如,Mary有泰国和越南国籍,所以我写了如下代码。

Sql Server数据库表:

```plaintext
Id    Name    Nationality
1     Mary    1,2
2     Andrew  4
3     Ken     3

期望结果:

1 Mary Vietnam,Thai
英文:

I am working on a project which used visual basic to write code in asp.net. I have some confusion when I am using the select case statement in asp.net. My asp.net is connected to sql server database, I have a table called Person, it has Id as decimal, Name as string and Nationality as string. In the table, it has some people had 2 nationality or above, I have case for nationality, for example, Mary have thai and vietnamese nationality, so i have written the code like this.

private sub Button1.Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click

Dim a() as string

a = Me.DropDownList1.SelectedValue.Split(",".ToCharArray)

Select Case CInt(a(0))
    Case 1
        TextBox1.Text= "Vietnam"
    Case 2
        TextBox2.Text= "Thai"
    Case 3
        TextBox3.Text= "Australia"
    Case 4
        TextBox4.Text= "Canada"
End Select

End Sub

Sql server database Table:

Id    Name    Nationality
1     Mary    1,2
2     Andrew  4
3     Ken     3

Expected Result:

1 Mary Vietname,Thai

答案1

得分: 1

以下是您要的翻译部分:

一旦您拆分了项目,您可以遍历它们,将每个文本片段添加到一个List(Of String),然后在最后将它们连接在一起。

也许是这样的:

Module Module1

    Public Function GetNationalityNames(csvIds As String) As String
        Dim natNums = csvIds.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
        Dim natNames As New List(Of String)

        For Each nat In natNums
            Select Case nat
                Case "1"
                    natNames.Add("越南")
                Case "2"
                    natNames.Add("泰国")
                Case "3"
                    natNames.Add("澳大利亚")
                Case "4"
                    natNames.Add("加拿大")
            End Select
        Next

        Return String.Join(", ", natNames)

    End Function

    Sub Main()
        Console.WriteLine(GetNationalityNames("3"))
        Console.WriteLine(GetNationalityNames("1,2"))

        Console.ReadLine()

    End Sub

End Module

输出:

澳大利亚
越南, 泰国

(您会将函数声明为Shared,但这只是一个快速的控制台程序,用于演示。)

英文:

Once you've split the items, you can iterate over them, adding each piece of text to a List(Of String), then join them together at the end.

Perhaps somthing like:

Module Module1

    Public Function GetNationalityNames(csvIds As String) As String
        Dim natNums = csvIds.Split({","c}, StringSplitOptions.RemoveEmptyEntries)
        Dim natNames As New List(Of String)

        For Each nat In natNums
            Select Case nat
                Case "1"
                    natNames.Add("Vietnam")
                Case "2"
                    natNames.Add("Thai")
                Case "3"
                    natNames.Add("Australia")
                Case "4"
                    natNames.Add("Canada")
            End Select
        Next

        Return String.Join(", ", natNames)

    End Function

    Sub Main()
        Console.WriteLine(GetNationalityNames("3"))
        Console.WriteLine(GetNationalityNames("1,2"))

        Console.ReadLine()

    End Sub

End Module

Outputs:

Australia
Vietnam, Thai

(You'd declare the function to be Shared, but that was just a quick console program to demonstrate.)

huangapple
  • 本文由 发表于 2023年6月5日 17:17:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76405008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定