简体   繁体   中英

How would I write this VB.Net loop functionally?

I have a VB.Net function that translates a NameValueColletion to an IDictionary . How would I write this functionally (with Select , etc. instead of a loop)?

<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
Return source.Cast(Of String)().
             ToDictionary(Function(key) key, Function(key) source(key))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM