7

尝试将 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
4

1 回答 1

6

试试这个

wit2.Where(Function(elem) elem.title="chuck").Count()

它比上面的要简单得多。

希望它会有所帮助

List 具有在 List 类中定义的 Count 属性和在 IEnumerable 上定义的 Count() 扩展方法。这似乎是多余的,但请记住,并非所有 IEnumerable 实现都定义了计数。

As any collection that implements ICollection or ICollection must specify a Count property. Since List, arrays, and many other collections implement ICollection, this means call Count directly and avoid calling the extension method.

于 2012-12-28T16:16:57.630 回答