return 的函数只是来自 KeyValuePair 的值

A Function to return just the value from a KeyValuePair

我有以下函数,它预先从数据库中获取一系列 KVP,现在我想编写一个函数,它将一个整数值作为其参数,return 匹配字符串值。请问最好的方法是什么?

Private Function GetReasons() As List(Of KeyValuePair(Of Integer, String))
    Dim returnValue As New List(Of KeyValuePair(Of Integer, String))
    Dim con As New SqlConnection(SCRGlobals.ConnectionString)
    Dim cmd As New SqlCommand("SELECT CODE, DESC FROM ltblDELAY", con)
    cmd.CommandType = CommandType.Text
    Try
        con.Open()
        Dim c As SqlDataReader = cmd.ExecuteReader()
        While c.Read
            returnValue.Add(New KeyValuePair(Of Integer, String)(c("CODE"), c("DESC")))
        End While
    Catch ex As Exception
        Common.LogError(ex, True, False)
    Finally
        If con.State <> ConnectionState.Closed Then
            con.Close()
        End If
    End Try
    Return returnValue
End Function

您应该将其作为过滤器参数传递给数据库:WHERE CODE=@Code:

Private Function GetReasons(code As Int32) As List(Of KeyValuePair(Of Integer, String))
    Dim returnValue As New List(Of KeyValuePair(Of Integer, String))
    Try
        Using con As New SqlConnection(ConnectionString)
            Using cmd As New SqlCommand("SELECT CODE, DESC FROM ltblDELAY WHERE CODE=@Code", con)
                cmd.Parameters.Add("@Code", SqlDbType.Int).Value = code
                con.Open()
                Using rd = cmd.ExecuteReader()
                    While rd.Read()
                        Dim desc = rd.GetString(1)
                        returnValue.Add(New KeyValuePair(Of Int32, String)(code, desc))
                    End While
                End Using
            End Using
        End Using
    Catch ex As Exception
        Common.LogError(ex, True, False)
    End Try
    Return returnValue
End Function

请注意,我还使用了 Using 语句来确保所有可能包含非托管资源(如连接)的对象都得到处置 属性(即使出现错误)。这也将关闭连接。

我还使用了 SqlDatareader.GetString 而不是 returns Object 的索引器。您的代码无法使用我强烈推荐的 Option Strict On 进行编译。


由于您只对字符串描述感兴趣,return a List(Of String):

Private Function GetReasons(code As Int32) As List(Of String)
    Dim returnValue As New List(Of String)
    Try
        Using con As New SqlConnection(ConnectionString)
            Using cmd As New SqlCommand("SELECT CODE, DESC FROM ltblDELAY WHERE CODE=@Code", con)
                cmd.Parameters.Add("@Code", SqlDbType.Int).Value = code
                con.Open()
                Using rd = cmd.ExecuteReader()
                    While rd.Read()
                        Dim desc = rd.GetString(1)
                        returnValue.Add(desc)
                    End While
                End Using
            End Using
        End Using
    Catch ex As Exception
        Common.LogError(ex, True, False)
    End Try
    Return returnValue
End Function

无需自己编写函数。

Dim l As List(Of KeyValuePair(Of Integer, String))
l = GetReasons()
Dim intKey as Integer = 1 '<-- whatever value you want to look up.
'This is your function...
Dim strValue as String = l.ToDictionary(Function(x) x.Key).Item(intKey).Value

实际上,您可以将它包装在您自己的函数中(并使其更健壮):

Private Function GetValue(l As List(Of KeyValuePair(Of Integer, String)), key As Integer)
    Dim d As Dictionary(Of Integer, KeyValuePair(Of Integer, String)) = l.ToDictionary(Function(x) x.Key)
    If d.ContainsKey(key) Then
        Return d.Item(key).Value
    Else
        'Default value or error message
    End If
End Function