获取 2 个列表之间的差异
本文关键字:之间 列表 获取 | 更新日期: 2023-09-27 18:35:46
我有两个列表(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
对于两个列表中可用的项目,我想从两个列表之间存在差异的项目的
ListA
(Login
字段是唯一键)获取一个列表。
示例:如果对于ListA
中的Login
"MyLogin",则FirstName
的值与ListB
中的值不匹配。Login
"我的登录"项目必须是结果列表的一部分。
示例:如果特定登录的ListA
和ListB
之间的Age
不同,则该项目必须是结果的一部分
谢谢。
要比较自定义数据类型列表的对象,您需要在类中实现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));
编辑
试试这个解决方案的细节差异:比较两个对象并找出差异
如何:查找两个列表之间的设置差异 (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;
var list3 = list1.Except(list2).ToList(); //List3 contains what in list1 but not _list2.
尝试这样做进行对象比较,并循环使用它进行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
}
}
}
}
你可以使用 LINQ 中的 Zip() 和 Intersect()