我知道您可以将 Any、Exists 和 Single 与 LINQ 一起使用,但无法完全实现。我需要根据 id 进行查找以查看它是否在数组中,并确保该值只有一个匹配项。因为如果有 2 个它会导致问题..我要检查的要求是数组中的每个 ID 只有一个且只有一个。
这是我尝试过的
if(someIntArray.Single(item => item = 3)
//... we found the value 8 in the array only once so now we can be confident and do something
最佳答案
下面是我将如何解决这个问题:
if (someIntArray.Count(item => item == 3) == 1)
{
//only one '3' found in the array
...
}
关于c# - 确定一个 int 值是否在数组中存在一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14270854/