Smith
Smith

Reputation: 5961

Count items in List(Of structure) using predicate in .NET 2.0/VB.NET

I need to count the items that meet a criteria in a List(Of Structure) in .NET 2.0. For example:

Dim listcars as New List(Of car)

Structure car
   Dim Name as String
   Dim year as Integer
End structure

Now I need to count all cars with name Toyota, etc.. How do I do it?

Upvotes: 1

Views: 8562

Answers (4)

Joel Coehoorn
Joel Coehoorn

Reputation: 416039

Dim toyotas As Integer = carList.Count(Function(c) c.Name = "Toyota")

Upvotes: 2

Mangesh
Mangesh

Reputation: 3997

Here you go.

 var count = carList.Count(x => x.Name == "Toyota");

Upvotes: 1

Hand-E-Food
Hand-E-Food

Reputation: 12814

You want List.LongCount.

Dim CarList As New List(Of Car)
Dim Model As String = "Toyota"
Dim ToyotaCount As Long = CarList.LongCount(Function(car) car.Name = Model)

Upvotes: 1

Dave
Dave

Reputation: 11162

The syntax is blatantly wrong, but something like this:

Dim toyotas as Integer;
toyotas = 0;
foreach(car c in listcars){
    if(c.Name == "toyota")//make sure to do string comparison here.
        toyotas++;
}

Upvotes: 0

Related Questions