找到同一类的两个对象的差异的最简单方法

时间:2018-01-23 16:14:38

标签: c# compare

所以我正在研究我的最后一年项目,我有一个算法来找出两条路线之间的差异。但是,我的算法太长了,只有在if else语句的地方200行的其他语句才有。我相信我没有想到的更容易,我会告诉你和例子。

 for (int x = 0; x < networkElements.Count - 1; x++)
        {
            for (int i = x + 1; i < networkElements.Count; i++)
            {
                if (networkElements.ElementAt(x).Description.Equals(networkElements.ElementAt(i).Description))
                {
                    if (networkElements.ElementAt(x).Port1.Equals(networkElements.ElementAt(i).Port1))
                    {
                        if (networkElements.ElementAt(x).Port2.Equals(networkElements.ElementAt(i).Port2))
                        {


                           Console.WriteLine("Both Routes are identical\n");
                            comparisonReults.Add(networkElements.ElementAt(x));
                        }
                        else
                        {
                            Console.WriteLine("Port 2 is different\n" +
                                "Port 2 for route 1 = " + networkElements.ElementAt(x).Port2 + " Port 2 for route 2 = " + networkElements.ElementAt(i).Port2 + "\n");
                           }
                    }
                    else
                    {
                        Console.WriteLine("Port 1 is different\n" +
                                "Port 1 for route 1 = " + networkElements.ElementAt(x).Port1 + " Port 1 for route 2 = " + networkElements.ElementAt(i).Port1 + "\n");

                        if (!networkElements.ElementAt(x).Port2.Equals(networkElements.ElementAt(i).Port2))
                        {
                            Console.WriteLine(" Also port 2 is different\n" +
                                "Port 2 for route 1 = " + networkElements.ElementAt(x).Port2 + " Port 2 for route 2 = " + networkElements.ElementAt(i).Port2 + "\n");
                            else
                        {
                           Console.WriteLine("\n port 2 the same port 1 is different");
                        }
                    }
                }
                else
                {
                    Console.WriteLine("\n completey different routes not eligable for comparison");

                    }

然而,我正在修改这个方法,而我想到的一些事情我不能用它:

if (!route1.Equal(route2)){
    //fill in here stackoverflow
    //is there a function for example route1.getDifference(route2)
    //built in function to compare properties
}
else{
     //they are identical
    }

0 个答案:

没有答案