获得2列出之间的差异 [英] Get the differences between 2 lists
问题描述
我有两个列表(利斯塔
和数组listB
),这些列表的类型是一样的 PersonInfo
的登录
字段是一个独特的密钥。
公共类PersonInfo
{
公共字符串登录{获得;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公众诠释年龄{获得;组; }
公共BOOL主动{获得;组; }
}
我想比较这两个清单:
-
我想获得的项目清单中
利斯塔
在数组listB
-
对于双方列出了可用的项目,我想从一个列表
利斯塔
(登录
字段是一个唯一的密钥),其中有两个列表之间的差的项。
例如:如果在利斯塔
的登录
MyLogin,的价值名字
不与数组listB
的值相匹配。以MyLogin为登录
的项目必须在结果列表中的一部分。
例如:如果年龄
的利斯塔
和数组listB
特定登录不同的是,该项目必须是结果的一部分
感谢。
要比较的自定义数据类型列表对象,您将需要实施 IEquatable
在你的类并覆盖 GetHash code()
勾选此 MSDN链接
您的类
公共类PersonInfo:IEquatable< PersonInfo>
{
公共字符串登录{获得;组; }
公共字符串名字{获得;组; }
公共字符串名字{获得;组; }
公众诠释年龄{获得;组; }
公共BOOL主动{获得;组; }
公共布尔等于(PersonInfo等)
{
//检查比较对象是否为空。
如果(Object.ReferenceEquals(其它,空))返回false;
//检查比较对象是否引用了相同的数据。
如果(Object.ReferenceEquals(这一点,其他))返回true;
//检查属性是否相等。
返回Login.Equals(other.Login)及&安培; FirstName.Equals(other.FirstName)及&安培; LastName.Equals(other.LastName)及&安培; Age.Equals(other.Age)及&安培; Active.Equals(other.Active);
}
公众覆盖INT GetHash code()
{
INT hashLogin =登录== NULL? 0:Login.GetHash code();
INT hashFirstName =姓== NULL? 0:FirstName.GetHash code();
INT hashLastName =名字== NULL? 0:LastName.GetHash code();
INT hashAge = Age.GetHash code();
INT hashActive = Active.GetHash code();
//计算散列code。
返程(hashLogin + hashFirstName + hashLastName)^(hashAge + hashActive);
}
}
然后这里是你如何使用它(如Pranay的答复上市)
名单,其中,PersonInfo>利斯塔=新的名单,其中,PersonInfo>(){新PersonInfo {登录=1,名字=詹姆斯,名字=沃森,主动= TRUE,年龄= 21},新PersonInfo {登录=2,名字=简,名字=莫里森,主动= TRUE,年龄= 25},新PersonInfo {登录=3,名字=金,名字=约翰,主动=假的,年龄= 33}} ;
名单< PersonInfo>数组listB =新的名单,其中,PersonInfo>(){新PersonInfo {登录=1,名字=James2222,名字=沃森,主动= TRUE,年龄= 21},新PersonInfo {登录=3,名字=金,名字=约翰,主动=假的,年龄= 33}};
//获取项目在利斯塔不在数组listB
名单< PersonInfo> FilteredListA = ListA.Except(数组listB).ToList();
//为了得到利斯塔和FilteredListA之间的差(距离FilteredListA项目将从利斯塔被删除)
ListA.RemoveAll(一个=> FilteredListA.Contains(一));
I have two lists (ListA
and ListB
), the type of these lists is the same PersonInfo
, the Login
field is a unique key.
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; }
}
I'd like compare these two lists :
I'd like get a list of items in
ListA
not available inListB
For the items available in both lists, I'd like get a list from
ListA
(Login
field is a unique key) for the item where there is a difference between the two lists.
Example : if for the Login
"MyLogin" in ListA
, the value of FirstName
does not match with the value in ListB
. The item with "MyLogin" as Login
must be part of the result list.
Example : if the Age
between ListA
and ListB
for a specific login is different, the item must be part of the result
Thanks.
To compare objects of custom data type lists, you will need to implement IEquatable
in your class and override GetHashCode()
Check this MSDN Link
Your class
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);
}
}
Then here is how you use it (as listed in Pranay's response)
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列出之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!