尝试将Count用作lambda时,我遇到了一个奇怪的错误
'Public ReadOnly Property Count As Integer'没有参数,其返回类型无法编入索引'
如果我算到LongCount它神奇地运行.根据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
Run Code Online (Sandbox Code Playgroud)
试试这个
wit2.Where(Function(elem) elem.title="chuck").Count()
Run Code Online (Sandbox Code Playgroud)
它比上面的要简单得多.
希望它会有所帮助
List具有List类中定义的Count属性和IEnumerable上定义的Count()扩展方法.这似乎是多余的,但请记住,并非所有IEnumerable实现都定义了计数.
由于任何实现ICollection或ICollection的集合都必须指定Count属性.由于List,数组和许多其他集合实现ICollection,这意味着直接调用Count并避免调用扩展方法.