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

尝试将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并避免调用扩展方法.

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...