3

我有两个列表(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. 对于两个列表中可用的项目,我想从ListALogin字段是唯一键)中获取两个列表之间存在差异的项目的列表。

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

示例:如果特定登录的Age介于ListA和之间不同,则该项目必须是结果的一部分ListB

谢谢。

4

5 回答 5

5

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

检查此MSDN 链接

你的班

    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));
于 2012-05-30T07:29:21.003 回答
4

编辑

试试这个解决方案的细节差异: 比较两个对象并找到差异


如何:查找两个列表之间的差集 (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;
于 2012-05-30T05:33:01.100 回答
2
var list3 = list1.Except(list2).ToList(); //List3 contains what in list1 but not _list2.
于 2014-12-20T21:50:58.260 回答
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
            }
        }
    }
}
于 2016-01-18T10:11:35.967 回答
-4

您可以使用 LINQ 中的 Zip() 和 Intersect()

于 2012-05-30T05:39:17.913 回答