Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb count Strange Behavior

Tags:

vb.net

I am getting a wierd error when trying to use Count as a lambda

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

If i Count to LongCount it magically works. According to this blog post 3 years ago this was a known issue. It seems it still is. My question is how do i resolve this issue?

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
like image 772
gh9 Avatar asked Dec 28 '12 15:12

gh9


1 Answers

try this

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

It is much simpler than above one.

hope it will help

List has both the Count property defined in the List class, and the Count() extension method defined on IEnumerable. This may seem redundant, but keep in mind that not all IEnumerable implementations have a count defined.

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.

like image 136
Paritosh Avatar answered Oct 03 '22 08:10

Paritosh