foreach 某些项目在 ListBox 中 - Winform,C#

龙乐君臣

我想知道如何找到某个项目出现在列表框中的次数

foreach (string Item in listBox1.Items) 
{
    Console.WriteLine("1 Item");
}
Console.ReadLine();

不幸的是,这会在整个 listBox 中循环。我只想要一个特定的项目。

莫西亚·塔博

假设listBox1.Items : List<string> or string[]...

选项1: listBox1.Items.Count(item => item == "YourCertainItem");

你可以使用 Linq

选项 2: listBox1.Items.Where(item => item == "YourCertainItem").Count();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章