我如何在 VB.net 中编写这个 lambda 选择方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10708158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write this lambda select method in VB.net?
提问by dotnetN00b
For I've tried this:
因为我试过这个:
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
But I'm getting a syntax error and all the examples that I've seen are in C#.
但是我遇到了语法错误,而且我看到的所有示例都是用 C# 编写的。
回答by Reed Copsey
This would be:
这将是:
Dim blah = exampleItems.Select (Function(x) New With { .Key = x.Key, .Value = x.Value }).ToList
For details, see Anonymous Types. (Depending on usage, you might also want Key or Value to be flagged with the Key keyword.)
有关详细信息,请参阅匿名类型。(根据使用情况,您可能还希望使用Key 关键字标记 Key 或 Value 。)
That being said, Dictionary(Of TKey, Of TValue)
already is an IEnumerable(Of KeyValuePair(Of TKey, Of TValue)
, so you can also just do:
话虽如此,Dictionary(Of TKey, Of TValue)
已经是一个IEnumerable(Of KeyValuePair(Of TKey, Of TValue)
,所以你也可以这样做:
Dim blah = exampleItems.ToList
And you'll have a list of KeyValuePair, which has a Key
and Value
property already. This really means there's no need to make the anonymous type.
并且您将拥有一个 KeyValuePair 列表,该列表已经具有Key
andValue
属性。这真的意味着不需要创建匿名类型。