比较两个列表时的结果很奇怪

JSJ*_*JSJ 2 .net c# .net-4.0

我想比较两个列表.我想检查List2是否包含List1中的任何项目.我得到意想不到的结果 请参阅下面的代码.

测试代码类

class Program
{
    static void Main(string[] args)
    {
        bool loop = true;
        int textcount = 1;
        while (loop)
        {

            var collection1 = GetCollection();
            var collection2 = GetCollection();

            Console.WriteLine("Test No " + textcount.ToString());


            Console.WriteLine("Collection 1 =" + String.Join(", ", collection1.ToArray()));

            Console.WriteLine("Collection 2 =" + String.Join(", ", collection2.ToArray()));

            System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
            watch.Start();
            var hasitem = collection1.Any(item => collection2.Contains(item));

            watch.Stop();

            Console.WriteLine(hasitem.ToString() + "  Found in " + watch.ElapsedTicks.ToString());
            watch.Reset();

            watch.Start();
            var hasAtLeastOne = collection1.Intersect(collection2).Any();
            watch.Stop();
            Console.WriteLine(hasAtLeastOne.ToString() + "  With Intersect Found in " + watch.ElapsedTicks.ToString());
            textcount++;
            Console.ReadKey();
        }


    }
    static Random ran = new Random();
    private static IEnumerable<int> GetCollection()
    {
        for (int i = 0; i < 5; i++)
        {
            yield return ran.Next(i, 20);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

结果非常烦人.看到最后4个结果.

Test No 1
Collection 1 =10, 8, 18, 6, 11
Collection 2 =3, 12, 18, 13, 6
True  Found in 3075
True  With Intersect Found in 15297
 Test No 2
Collection 1 =18, 13, 7, 18, 5
Collection 2 =12, 18, 8, 3, 5
True  Found in 22
True  With Intersect Found in 100
 Test No 3
Collection 1 =1, 6, 15, 7, 9
Collection 2 =16, 15, 14, 14, 12
True  Found in 21
True  With Intersect Found in 23
 Test No 4
Collection 1 =3, 16, 7, 4, 19
Collection 2 =6, 3, 15, 15, 9
True  Found in 21
True  With Intersect Found in 56
 Test No 5
Collection 1 =18, 18, 9, 17, 10
Collection 2 =17, 12, 4, 3, 11
True  Found in 25
True  With Intersect Found in 20
 Test No 6
Collection 1 =9, 9, 2, 17, 19
Collection 2 =17, 2, 18, 3, 15
False  Found in 109
False  With Intersect Found in 41
 Test No 7
Collection 1 =3, 15, 3, 5, 5
Collection 2 =2, 2, 11, 7, 6
True  Found in 22
False  With Intersect Found in 15
 Test No 8
Collection 1 =7, 14, 17, 14, 18
Collection 2 =18, 4, 7, 18, 16
False  Found in 28
True  With Intersect Found in 19
 Test No 9
Collection 1 =3, 9, 6, 18, 9
Collection 2 =10, 3, 17, 17, 18
True  Found in 28
True  With Intersect Found in 22
 Test No 10
Collection 1 =15, 18, 2, 9, 8
Collection 2 =10, 15, 3, 10, 19
False  Found in 135
True  With Intersect Found in 128
 Test No 11
Collection 1 =6, 2, 17, 18, 18
Collection 2 =14, 16, 14, 6, 4
False  Found in 20
False  With Intersect Found in 17
Run Code Online (Sandbox Code Playgroud)

Dan*_*rth 11

问题是你所谓的"集合"实际上是一个不稳定的项目序列,每次枚举时它都会发生变化.这样做的原因是您实施的方式GetCollection.使用yield return基本上返回关于如何创建序列的蓝图.它不会返回序列本身.
因此,每次枚举"蓝色打印"时,它都会用于创建新序列.您只需输出两次"集合"即可验证这一点.您将看到每次值都不同.

这就是为什么你的测试产生完全任意结果的原因:你枚举每个集合三次:

  1. 将其输出到控制台时会发生第一次枚举
  2. 第二次枚举发生在带Any和的测试中Contains.因为这会启动一个新的枚举,所以会生成新的随机数.
  3. 第三次枚举发生在Intersect测试中.这会创建另一组随机数.

要修复它,请通过调用ToArray()结果来创建一个稳定的序列GetCollection.