一个函数只返回KeyValuePair的值

时间:2015-10-27 12:04:53

标签: vb.net asp.net-4.0 keyvaluepair

我有以下函数从数据库中获取一系列KVP,现在我想编写一个函数,它将以Integer值作为参数并返回匹配的String值。请问最好的方法是什么?

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

2 个答案:

答案 0 :(得分:1)

无需编写自己的功能。

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

答案 1 :(得分:0)

您应该将其作为filter-parameter传递给数据库: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而不是返回Object的索引器。您强烈建议使用Option Strict On编译代码。

由于您只对作为字符串的描述感兴趣,因此请返回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