Two comparison List data comparison
List inherits Enumerable, and there is an Except method in Enumerable,
which has two implementations:
- The first implementation generates the difference of two sequences by comparing the values using the default equality comparer.
- The second implementation generates the difference of two sequences by comparing the values using the specified IEqualityComparer.
This is using the default equality comparer.
This method is generally used to compare simple array or string content.
double[] numbers1 = {
2.0, 2.0, 2.1, 2.2, 2.3, 2.3, 2.4, 2.5 };
double[] numbers2 = {
2.2 };
IEnumerable<double> onlyInFirstSet = numbers1.Except(numbers2);
foreach (double number in onlyInFirstSet)
Console.WriteLine(number);
Here's the output:
2
2.1
2.3
2.4
2.5
Use the specified IEqualityComparer to compare values,
which can be used to compare object collections
Model
namespace 测试项目.Test
{
public class test{
public int id{
get;set;}
public string name{
get;set;}
public string tel{
get;set;}
public int age{
get;set;}
public string clas{
get;set;}
}
public class testComparer : IEqualityComparer<test>
{
public bool Equals(test x, test y)
{
return x.name== y.name && x.tel== y.tel;
}
public int GetHashCode(test obj)
{
return obj.name.GetHashCode() ^ obj.tel.GetHashCode();
}
}
}
Instructions:
List<test> list=...这是第一个对象集合
List<test> list2=...这是第二个集合
var different = list.Except(list2,new testComparer()).ToList();
这是查询第一个集合里有但是第二个集合里面没有的数据