IEnumerable.哪里丢了东西

本文关键字:丢了 IEnumerable | 更新日期: 2023-09-27 18:18:39

我注意到我的代码中有一个问题,其中IEnumerable似乎不包含我想要的所有元素,这里是代码(它在foreach内,这是我从哪里得到_Lis):

IEnumerable<Pot> result = pots.Where(e => SignArray(e.PotVal) == _Lis.ToArray());
Console.WriteLine("Result count:" + result.Count());
Console.WriteLine(JObject.FromObject(new { test1 = SignArray(pots[0].PotVal) }));
Console.WriteLine(JObject.FromObject(new { test2 = _Lis.ToArray() }));
结果:

Result count: 0
{
  "test1": [
    0.0,
    0.0,
    0.0,
    1.0,
    0.0,
    0.0,
    0.0,
    1.0,
    0.0
  ]
}
{
  "test2": [
    0.0,
    0.0,
    0.0,
    1.0,
    0.0,
    0.0,
    0.0,
    1.0,
    0.0
  ]
}

IEnumerable.哪里丢了东西

您的两个数组不是同一个对象。它们包含相同的对象。使用Enumerable.SequenceEqual代替operator ==,比较引用是否相等(即变量是否指向相同的内存地址?)

IEnumerable<Pot> result = pots.Where(e => SignArray(e.PotVal).SequenceEqual(_Lis.ToArray()));