【发布时间】:2016-09-22 07:29:24
【问题描述】:
我无法理解 linq any 运算符。让我们考虑以下 sn-p 代码(使用 VS 2010 和 .Net 4.0)
List<string> sample = new List<string> { "a", "b", "c", "d" };
List<string> secondSample = new List<string> { "b", "c" };
foreach (string s in sample)
{
if(secondSample.Any(data=> data.ToString() == s))
Console.WriteLine(s);
}
运行时会产生以下输出
b
c
这是我所期待的。但是,如果我将相等运算符 (==) 更改为 Not Equal(!=) 我会得到这个
a
b
c
d
不应该这样
a
d
如果我将 if 条件更改为
if(!(secondSample.Any(data=> data.ToString() == s)))
我明白了
a
d
所以我的问题是我是否以错误的方式解释 Any 运算符?不应该
if(secondSample.Any(data=> data.ToString() != s))
当来自secondSample 的值不在样本中时,评估为真
【问题讨论】: