我知道哪些项目不一样我怎么知道什么是相同的?

时间:2015-01-31 12:51:30

标签: c# .net winforms

if (pixelscoordinatesinrectangle != null && newpixelscoordinates != null)
                {
                    IEnumerable<Point> differenceQuery =
                    pixelscoordinatesinrectangle.Except(newpixelscoordinates);

                    // Execute the query.
                    foreach (Point s in differenceQuery)
                        w.WriteLine("The following points are not the same" + s);
                }

我使用除了foreach之外的东西,然后写下不相同的坐标。

pixelcoordinatesinrectangle和newpixelscoordinates都是List

现在我想要做的是写入文本文件(w)不同的东西,只写那些是相同的。

我在两个列表中的意思相同,变量s是相同的。

现在例如,如果s是:X = 200 Y = 100我知道它们在两个列表中都不是相同的坐标。

但现在我想改变它,所以变量s只包含两个列表中相同的变量。

例如,如果一个列表中的索引0在另一个列表中的索引0中包含相同的坐标,则将该坐标写为相同。

但是如果一个包含其中一个列表中的坐标的索引与另一个列表中的坐标不同,那么中断就会停止该过程。

只有当整个列表完全相同时,我才需要在文本文件的末尾写入!不仅是一个索引(项目),而且只有整个列表相同。

1 个答案:

答案 0 :(得分:1)

以下代码应该找到两个列表中都存在的项目

static void Main(string[] args)
        {
            var pixelscoordinatesinrectangle = new List<Point>() { new Point(200, 100), new Point(100, 100), new Point(200, 200) };
            var newpixelscoordinates = new List<Point>() { new Point(200, 100), new Point(400, 400) };

            FindMatchingPoints(pixelscoordinatesinrectangle, newpixelscoordinates);

            Console.ReadKey();
        }

        private static void FindMatchingPoints(List<Point> pixelscoordinatesinrectangle, List<Point> newpixelscoordinates)
        {
            if (pixelscoordinatesinrectangle != null && newpixelscoordinates != null)
            {
                IEnumerable<Point> matchingPoints = pixelscoordinatesinrectangle.Where(p => newpixelscoordinates.Contains(p));

                // Execute the query.
                foreach (Point s in matchingPoints)
                    Console.WriteLine("The following points are the same" + s);
            }

        }
相关问题