vb.net - VB.net 使用 Lambda 表达式迭代匿名类型集合
问题描述
我试图遍历匿名类型集合,但得到 System.MissingMemberException HResult=0x80131512 Message=Overload resolution failed 因为没有可访问的“ForEach”接受这个数量的参数。并且无法确定原因。我成功使用 For Each 但无法使用 Lambda 表达式。
Dim car As Object = {(New With {Key .Model = "Buick", .Color = "Blue"}),
(New With {Key .Model = "Volvo", .Color = "Green"}),
(New With {Key .Model = "Jeep", .Color = "Red"})}
For Each item In car
If item.Color = "Blue" Then Debug.Print(String.Format("{0} {1}", item.Model, item.Color))
Next
car.ForEach(Sub(x)
Debug.Print(String.Format("[0} {1}", x.model, x.color))
End Sub)
解决方案
ForEach 是 List(Of T) 的东西,而不是 LINQ 的东西..
Dim cars = ({ New With {Key .Model = "Buick", .Color = "Blue"},
New With {Key .Model = "Volvo", .Color = "Green"},
New With {Key .Model = "Jeep", .Color = "Red"}
}).ToList()
这会将汽车创建为匿名数组,然后使用 ToList 从中创建一个列表;然后可以使用 ForEach
注意:集合使用复数
推荐阅读
- html - Bootstrap 模态背景
- r - 将字符 df 转换为列
- pivot-table - 独立于切片器的 Dax 数据透视表总和
- sql - 如何从多个 select 语句创建 SQL 查询结果(没有 UNION)
- symfony - 无法使用 phpunit 在 symfony 中扩展泛型类
- upgrade - 如何将 .Net 框架 4.6.2 升级到 4.7
- php - 安全 HTML 和 PhP
- javascript - 如何在 Vue.js 中触发点击 ref
- mysql - 使用 vb.net 插入新的 SQL 数据后如何刷新我的 datagridview
- java - 改造 - 是否有与标题等效的“@Path”?