我有一个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
答案 0 :(得分:3)
Return source.Cast(Of String)().
ToDictionary(Function(key) key, Function(key) source(key))