获取2个列表之间的差异
我有两个列表( ListA
和ListB
),这些列表的类型是相同的PersonInfo
, Login
字段是唯一键。
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; } }
我想比较这两个列表:
-
我想获得
ListA
不可用的ListB
中的项目列表 -
对于两个列表中可用的项目,我想从列表中获取列表(
Login
字段是唯一键),这两个列表之间存在差异。
示例:如果对于ListA中的Login
“MyLogin”, ListA
的值与ListA
中的值不匹配。 “MyLogin”作为Login
项的项必须是结果列表的一部分。
示例:如果特定登录的ListA
和ListB
之间的Age
不同,则该项必须是结果的一部分
谢谢。
要比较自定义数据类型列表的对象,您需要在类中实现IEquatable
并覆盖GetHashCode()
检查此MSDN链接
你的class
public class PersonInfo : IEquatable { 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 ListA = new List () { 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 ListB = new List () { 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 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));
编辑
尝试这个解决细节差异: 比较两个对象并找出差异
如何:找到两个列表之间的集合差异(LINQ)
Enumerable.Except方法(IEnumerable,IEnumerable) -使用默认的相等比较器来比较值,生成两个序列的集合差异。
double[] numbers1 = { 2.0, 2.1, 2.2, 2.3, 2.4, 2.5 }; double[] numbers2 = { 2.2 }; IEnumerable onlyInFirstSet = numbers1.Except(numbers2);
要么
//newList will include all the common data between the 2 lists List newList = list1.Intersect(list2).ToList (); //differences will be the data not found List 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;
var list3 = list1.Except(list2).ToList(); //List3 contains what in list1 but not _list2.
尝试使用它进行对象比较,并为List
循环它
public static void GetPropertyChanges(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 } } } }
您可以使用LINQ中的Zip()和Intersect()