使用 .NET 2.0/VB.NET 中的谓词对列表(结构)中的项目进行计数

发布于 2024-11-25 22:40:34 字数 236 浏览 4 评论 0原文

我需要计算 .NET 2.0 中的 List(Of Structure) 中满足条件的项目。例如:

Dim listcars as New List(Of car)

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

现在我需要统计所有名称为丰田等的汽车。我该怎么做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

李白 2024-12-02 22:40:35
Dim toyotas As Integer = carList.Count(Function(c) c.Name = "Toyota")
Dim toyotas As Integer = carList.Count(Function(c) c.Name = "Toyota")
扎心 2024-12-02 22:40:35

您需要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)

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)
忆沫 2024-12-02 22:40:35

干得好。

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

Here you go.

 var count = carList.Count(x => x.Name == "Toyota");
坠似风落 2024-12-02 22:40:35

语法明显错误,但类似这样:

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

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++;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文