如何在VB.net中编写这个lambda select方法?

dot*_*00b 22 linq vb.net lambda .net-3.5

因为我试过这个:

Dim exampleItems As Dictionary(Of String, String) = New Dictionary(Of String, String)
Dim blah = exampleItems.Select (Function(x) New (x.Key, x.Value)).ToList 'error here
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个语法错误,我见过的所有例子都是在C#中.

Ree*_*sey 38

这将是:

Dim blah = exampleItems.Select (Function(x) New With { .Key = x.Key, .Value = x.Value }).ToList 
Run Code Online (Sandbox Code Playgroud)

有关详细信息,请参阅匿名类型.(根据用途,您可能还希望使用Key关键字标记Key或Value .)

话虽如此,Dictionary(Of TKey, Of TValue)已经是一个IEnumerable(Of KeyValuePair(Of TKey, Of TValue),所以你也可以这样做:

Dim blah = exampleItems.ToList
Run Code Online (Sandbox Code Playgroud)

你就会有KeyValuePair,其中有一个列表KeyValue属性了.这实际上意味着没有必要制作匿名类型.