比较两个列表的区别 [英] Compare two Lists for differences

查看:189
本文介绍了比较两个列表的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对我们如何才能最好写一个通用的功能,使两个列表进行比较一些反馈。该列表包含类对象,我们想遍历一个列表,寻找同一项目的第二列表,并报告任何差异。

我们已经有一个方法来比较类,因此我们需要了解如何,我们可以从两个列表喂养方法(如下图所示)的反馈。

举例来说,假设我们有一个有三个属性,名称,标识,系一个简单的员工类。我们要报告列表和另一个列表之间的差异。

注:
两个列表总是包含相同的项目数。

正如上面提到的,我们有我们用来比较两个班,我们如何能够把这种方法以应付列表,即从另一种方法,遍历目录和饲料类泛型方法泛型方法.. ..但我们如何找到等价类中的第二个列表传递到下面的方法;

 公共静态字符串CompareTwoClass_ReturnDifferences< T1,T2>(T1原稿,T2目的地)
    其中T1:类
    其中T2:类
{
    //如果需要实例化
    如果(目标== NULL)抛出新ArgumentNullException(目的地,目标类必须首先被实例化。);

    VAR差异= CoreFormat.StringNoCharacters;

    //循环遍历目标的每个属性
    的foreach(VAR DestProp在Dest.GetType()。GetProperties中())
    {
        //发现在原稿类的匹配性和比较
        的foreach(VAR OrigProp在Orig.GetType()。GetProperties中())
        {

            如果(OrigProp.Name = DestProp.Name || OrigProp.PropertyType = DestProp.PropertyType!)继续;
            如果(OrigProp.GetValue(原稿,空)的ToString()!= DestProp.GetValue(目的地,空)的ToString())
                差异=差异== CoreFormat.StringNoCharacters
                    ?的String.Format({0}:{1}  - > {2},OrigProp.Name,
                                                       OrigProp.GetValue(原稿,空)
                                                       DestProp.GetValue(目的地,空))
                    :的String.Format({0} {1} {2} {3}  - > {4},差异,
                                                              Environment.NewLine,
                                                              OrigProp.Name,
                                                              OrigProp.GetValue(原稿,空)
                                                              DestProp.GetValue(目的地,空));
        }
    }
    返回的差异;
}
 

任何建议或想法AP preciated?

编辑:针对.NET 2.0,以便LINQ是出了问题。

解决方案
  

....但我们如何找到等价类中的第二个列表传递给方法   以下;

这是你的实际问题;你必须至少有一个不变的属性,一个ID或类似的东西,找出两个列表中相应的对象。如果没有这样的属性,你,也解决不了这个问题没有错误。你可以只尝试猜测通过搜索最小的或逻辑的改变相应的对象。

如果您有这样的属性,该解决方案变得非常简单。

  Enumerable.Join(
   为listA,数组listB,
   A => a.Id,B =>出价,
   (A,B)=> CompareTwoClass_ReturnDifferences(A,B))
 


  

感谢你们danbruc和诺多的回馈。既解释将是相同的   长度和以相同的顺序。所以上述方法是接近的,但你可以修改此   方法将enum.Current传递到我上面贴的方法?

现在我很困惑...什么是什么问题?为什么不干脆以下?

 的(我的Int32 = 0; I< Math.Min(listA.Count,listB.Count);我++)
{
    收益回报CompareTwoClass_ReturnDifferences(listA的[I],数组listB [I]);
}
 

在Math.Min()调用甚至可能被排除在外,如果相等长度guaranted。


诺多的实现是因为委托并使用​​枚举,而不是使用的ICollection的当然是聪明的。

I would like some feedback on how we can best write a generic function that will enable two Lists to be compared. The Lists contain class objects and we would like to iterate through one list, looking for the same item in a second List and report any differences.

We already have a method to compare classes, so we need feedback on how we can feed the method (shown below) from two Lists.

For example, say we have a simple "Employee" class that has three properties, Name, ID, Department. We want to report the differences between List and another List.

Note:
Both lists will always contain the same number of items.

As mentioned above, we have a generic method that we use to compare two classes, how can we incorporate this method to cater for Lists, i.e. from another method, loop through the List and feed the classes to the generic method .... but how do we find the equivalent class in the second List to pass to the method below;

public static string CompareTwoClass_ReturnDifferences<T1, T2>(T1 Orig, T2 Dest)
    where T1 : class
    where T2 : class
{
    // Instantiate if necessary
    if (Dest == null) throw new ArgumentNullException("Dest", "Destination class must first be instantiated.");

    var Differences = CoreFormat.StringNoCharacters;

    // Loop through each property in the destination  
    foreach (var DestProp in Dest.GetType().GetProperties())
    {
        // Find the matching property in the Orig class and compare
        foreach (var OrigProp in Orig.GetType().GetProperties())
        {

            if (OrigProp.Name != DestProp.Name || OrigProp.PropertyType != DestProp.PropertyType) continue;
            if (OrigProp.GetValue(Orig, null).ToString() != DestProp.GetValue(Dest, null).ToString())
                Differences = Differences == CoreFormat.StringNoCharacters 
                    ? string.Format("{0}: {1} -> {2}", OrigProp.Name,
                                                       OrigProp.GetValue(Orig, null),
                                                       DestProp.GetValue(Dest, null)) 
                    : string.Format("{0} {1}{2}: {3} -> {4}", Differences,
                                                              Environment.NewLine,
                                                              OrigProp.Name,
                                                              OrigProp.GetValue(Orig, null),
                                                              DestProp.GetValue(Dest, null));
        }
    }
    return Differences;
}

Any suggestions or ideas appreciated?

Edit: Targeting .NET 2.0 so LINQ is out of the question.

解决方案

.... but how do we find the equivalent class in the second List to pass to the method below;

This is your actual problem; you must have at least one immutable property, a id or something like that, to identify corresponding objects in both lists. If you do not have such a property you, cannot solve the problem without errors. You can just try to guess corresponding objects by searching for minimal or logical changes.

If you have such an property, the solution becomes really simple.

Enumerable.Join(
   listA, listB,
   a => a.Id, b => b.Id,
   (a, b) => CompareTwoClass_ReturnDifferences(a, b))


thanks to you both danbruc and Noldorin for your feedback. both Lists will be the same length and in the same order. so the method above is close, but can you modify this method to pass the enum.Current to the method i posted above?

Now I am confused ... what is the problem with that? Why not just the following?

for (Int32 i = 0; i < Math.Min(listA.Count, listB.Count); i++)
{
    yield return CompareTwoClass_ReturnDifferences(listA[i], listB[i]);
}

The Math.Min() call may even be left out if equal length is guaranted.


Noldorin's implementation is of course smarter because of the delegate and the use of enumerators instead of using ICollection.

这篇关于比较两个列表的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆