尝试使用 Count 作为 lambda 时出现奇怪的错误
'Public ReadOnly Property Count As Integer' Has no parameters and its return type cannot be indexed'
如果我 计数 至 LongCount 它神奇地起作用。根据 this blog post 3 年前,这是一个已知问题。看来还是这样。 我的问题是我该如何解决这个问题?
Module Module1
Sub Main()
Dim wit2 As New List(Of TestCount) From {New TestCount With {.title = "foo" _
,.PartNumber = "bar"} _
, New TestCount With {.title = "chuck" _
, .PartNumber = "norris"}}
Console.WriteLine(wit2.Count(Function(x) x.title = "chuck"))
End Sub
Friend Class TestCount
Property title As String
Property PartNumber As String
End Class
End Module
最佳答案
尝试这个
wit2.Where(Function(elem) elem.title="chuck").Count()
它比上面的简单得多。
希望它会有所帮助
List 既有在 List 类中定义的 Count 属性,也有在 IEnumerable 上定义的 Count() 扩展方法。这似乎是多余的,但请记住,并非所有 IEnumerable 实现都定义了计数。
因为任何实现 ICollection 或 ICollection 的集合都必须指定一个 Count 属性。由于 List、数组和许多其他集合都实现了 ICollection,这意味着直接调用 Count,避免调用扩展方法。
关于vb.net - vb 计数奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072058/