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

标签 vb.net

尝试使用 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/

相关文章:

vb.net - 使用 LINQ 从多个表中选择全部

.net - 在 .NET 中模拟 VBA 算术

c# - 在 GridView 中的 ASCX 控件内的控件上使用 Javascript 显示隐藏。 (ASP.NET + JavaScript)

vb.net - LinqDataSource 抛出 System.Data.Linq.Mapping.DataAttribute is not defined 错误

vb.net - 增加行大小取决于 DataGridView 中的图像

mysql - 检查连接时无法连接到任何指定的 MySQL 主机

asp.net - onClick 事件不会在 Firefox 或 Chrome 中触发,仅在 IE 中触发

vb.net - .Net 异步委托(delegate)中止

vb.net - 从文本文件修复区分大小写的搜索?

vb.net - VS2015 Goto Definition 显示元数据而不是对象浏览器