vb.net – vb计数奇怪的行为

前端之家收集整理的这篇文章主要介绍了vb.net – vb计数奇怪的行为前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
尝试将Count用作lambda时,我遇到了一个奇怪的错误

‘Public ReadOnly Property Count As Integer’ Has no parameters and its
return type cannot be indexed’

如果我算上LongCount,它就会神奇地起作用.根据@L_502_0@ 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相关文章