我如何在功能上编写这个VB.Net循环?

时间:2013-01-15 16:06:28

标签: vb.net linq

我有一个VB.Net函数,可以将NameValueColletion转换为IDictionary。我如何在功能上写这个(用Select等代替循环)?

<Extension()>
Public Function ToDictionary(ByVal source As NameValueCollection)
        As IDictionary(Of String, String)
    Dim ret = New Dictionary(Of String, String)
    Dim keys = source.Keys
    For Each key In keys
        Dim sKey = TryCast(key, String)
        Dim sVal = TryCast(source(key), String)
        If sKey Is Nothing OrElse sVal Is Nothing Then Continue For
        ret(sKey) = sVal
    Next
    Return ret
End Function

1 个答案:

答案 0 :(得分:3)

Return source.Cast(Of String)().
             ToDictionary(Function(key) key, Function(key) source(key))