我试图遍历一个匿名类型集合,但得到一个 System.MissingMemberException HResult=0x80131512 Message=重载解析失败,因为没有可访问的“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)
答案 0 :(得分:1)
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 然后可用
注意:集合使用复数
答案 1 :(得分:0)
感谢您为我指明了正确的方向,这是我正在寻找的工作示例。
Imports System
Imports System.Linq
Public Module Module1
Public Sub Main()
Dim cars = ({ New With { .Model = "Buick", .Color = "Blue"},
New With { .Model = "Volvo", .Color = "Green"},
New With { .Model = "Jeep", .Color = "Red"}
})
cars.AsEnumerable.ToList().ForEach(sub(x)
console.WriteLine(string.format("Model: {0} Color: {1}", x.Model, x.Color ))
End Sub)
End Sub
End Module