简体   繁体   English

在数组匹配查询中查找元素数

[英]Find number of elements in array matching query

I have the following class: 我有以下课程:

public class Words
{
    public string word;
    public bool correct;
}

and I have an array of Words. 而且我有很多单词。

Words[] words;

I wish to count how many elements in my array have the property correct set to true. 我希望计算数组中有多少个元素的correct属性设置为true。

What would be the most efficient way to do this? 什么是最有效的方法?

Using LINQ 使用LINQ

words.Where(w=>w.correct);

That should do the trick to filter. 那应该可以过滤。


But if you only want to count: 但是,如果您只想计算:

words.Count(w=>w.correct);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM