获取2个列表之间的差异

时间:2012-05-30 05:26:28

标签: c# .net linq

我有两个列表(ListAListB),这些列表的类型相同PersonInfoLogin字段是唯一键。

public class PersonInfo
{
    public string Login { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
    public bool Active { get; set; }
}

我想比较这两个列表:

  1. 我想获得ListA中未提供ListB

  2. ListA项目列表
  3. 对于两个列表中的可用项目,我想从LoginLogin字段是一个唯一键)获取该项目的列表,其中两者之间存在差异列表。

  4. 示例:如果ListA中的FirstName“MyLogin”,则ListB的值与Login中的值不匹配。 “MyLogin”为Age的项目必须是结果列表的一部分。

    示例:如果特定登录的ListAListB之间的{{1}}不同,则该项必须是结果的一部分

    感谢。

5 个答案:

答案 0 :(得分:4)

修改

尝试这个解决细节差异: Compare two objects and find the differences


How to: Find the Set Difference Between Two Lists (LINQ)

Enumerable.Except Method (IEnumerable, IEnumerable) - 使用默认的相等比较器来比较两个序列的集合差异。

       double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 };
        double[] numbers2 = { 2.2 };

        IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);

//newList will include all the common data between the 2 lists
List<T> newList = list1.Intersect(list2).ToList<T>();


//differences will be the data not found 
List<T> differences = list1.RemoveAll(a => newList.Contains(a));

外部联接以获得差异

var compare1to2 = from a in 
           from b in driveList2.Where(b => b.property == a.property).DefaultIfEmpty()
                              select a;

答案 1 :(得分:4)

要比较自定义数据类型列表的对象,您需要在班级中实施IEquatable并覆盖GetHashCode()

选中此MSDN Link

你的班级

    public class PersonInfo : IEquatable<PersonInfo>
    {
        public string Login { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public bool Active { get; set; }

        public bool Equals(PersonInfo other)
        {
            //Check whether the compared object is null.
            if (Object.ReferenceEquals(other, null)) return false;

            //Check whether the compared object references the same data.
            if (Object.ReferenceEquals(this, other)) return true;

            //Check whether the properties are equal.
            return Login.Equals(other.Login) && FirstName.Equals(other.FirstName) && LastName.Equals(other.LastName) && Age.Equals(other.Age) && Active.Equals(other.Active);
        }

        public override int GetHashCode()
        {

            int hashLogin = Login == null ? 0 : Login.GetHashCode();

            int hashFirstName = FirstName == null ? 0 : FirstName.GetHashCode();

            int hashLastName = LastName == null ? 0 : LastName.GetHashCode();

            int hashAge = Age.GetHashCode();

            int hashActive = Active.GetHashCode();

            //Calculate the hash code.
            return (hashLogin + hashFirstName + hashLastName) ^ (hashAge + hashActive);
        }
    }

然后是你如何使用它(如Pranay的回应中所列)

            List<PersonInfo> ListA = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "2", FirstName = "Jane", LastName = "Morrison", Active = true, Age = 25 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } };
            List<PersonInfo> ListB = new List<PersonInfo>() { new PersonInfo { Login = "1", FirstName = "James2222", LastName = "Watson", Active = true, Age = 21 }, new PersonInfo { Login = "3", FirstName = "Kim", LastName = "John", Active = false, Age = 33 } };

            //Get Items in ListA that are not in ListB
            List<PersonInfo> FilteredListA = ListA.Except(ListB).ToList();

            //To get the difference between ListA and FilteredListA (items from FilteredListA will be removed from ListA)
            ListA.RemoveAll(a => FilteredListA.Contains(a));

答案 2 :(得分:2)

var list3 = list1.Except(list2).ToList(); //List3 contains what in list1 but not _list2.

答案 3 :(得分:0)

尝试使用此对象进行比较并围绕它List<T>

循环
public static void GetPropertyChanges<T>(this T oldObj, T newObj)
{
    Type type = typeof(T);
    foreach (System.Reflection.PropertyInfo pi in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
    {
        object selfValue = type.GetProperty(pi.Name).GetValue(oldObj, null);
        object toValue = type.GetProperty(pi.Name).GetValue(newObj, null);
        if (selfValue != null && toValue != null)
        {
            if (selfValue.ToString() != toValue.ToString())
            {
                //do your code
            }
        }
    }
}

答案 4 :(得分:-4)

您可以使用LINQ

中的Zip()和Intersect()