【问题标题】:How do I write this lambda select method in VB.net?如何在 VB.net 中编写这个 lambda 选择方法?
【发布时间】:2012-05-29 07:20:14
【问题描述】:

因为我已经尝试过了:

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

但是我遇到了一个语法错误,而且我看到的所有示例都是用 C# 编写的。

【问题讨论】:

    标签: vb.net linq .net-3.5 lambda


    【解决方案1】:

    这将是:

    Dim blah = exampleItems.Select (Function(x) New With { .Key = x.Key, .Value = x.Value }).ToList 
    

    详情请见Anonymous Types。 (根据使用情况,您可能还希望使用 Key keyword 标记 Key 或 Value。)

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

    Dim blah = exampleItems.ToList
    

    您将获得一个 KeyValuePair 列表,其中已有 KeyValue 属性。这确实意味着不需要创建匿名类型。

    【讨论】:

    • +1 我正要发布完全相同的答案,但后来决定按 F5 这样做,并看到你的答案已经存在 :)
    猜你喜欢
    • 2011-11-02
    • 2015-02-20
    • 2013-03-20
    • 2010-10-30
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多