1.此接口用于对集合 的自定义相等比较算法的实现。包含2个方法:
Equals(T,T): 确定指定的对象是否相等。 T 为要比较的对象类型;
GetHashCode(T) :返回指定对象的哈希代码。如果两个对象的Equal
比较结果相等,则他们hashCode返回的对象也必须返回同一个值;
2.官方建议继承 EqualityComparer
3.
Union()
这个方法将会Union(并集)两个序列(集合)连接成一个新列表(集合)
public static IEnumerableUnion (this IEnumerable first, IEnumerable second) public static IEnumerable Union (this IEnumerable first,IEnumerable second, IEqualityComparer comparer)123
Intersect ()
它将产生两个序列的交集.
方法定义是:
public static IEnumerableIntersect (this IEnumerable first, IEnumerable second) public static IEnumerable Intersect (this IEnumerable first, Enumerable second,IEqualityComparer comparer) 1234
Except ()
它是从一个集合中删除存在另一个集合中的项.两个序列产生的集合差. 英文意思是:除此之外
方法定义是:
public static IEnumerableExcept (this IEnumerable first, IEnumerable second) public static IEnumerable Except (this IEnumerable first, IEnumerable second, IEqualityComparer comparer)12
实例代码分别如下:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;
namespace Test
{
class Program
{ static void Main(string[] args)
{
IList oneStudents = new List();
oneStudents.Add(new Student(1, false, "小新1", "徐汇"));
oneStudents.Add(new Student(2, false, "小新2", "闵行"));
oneStudents.Add(new Student(3, false, "小新3", "嘉定"));
oneStudents.Add(new Student(4, false, "小新4", "闸北"));
IList twoStudents = new List();
twoStudents.Add(new Student(5, false, "小新5", "贵州"));
twoStudents.Add(new Student(6, false, "小新6", "湖北"));
twoStudents.Add(new Student(7, false, "小新7", "山东"));
twoStudents.Add(new Student(8, false, "小新8", "西藏"));
IList threeStudents = new List();
threeStudents.Add(new Student(1, false, "小新1", "徐汇"));
threeStudents.Add(new Student(2, false, "小新2", "闵行")); var bingji = oneStudents.Union(twoStudents, new StudentListEquality()).ToList();//并(全)集
var jiaoji = oneStudents.Intersect(threeStudents, new StudentListEquality()).ToList();//交集
var chaji = oneStudents.Except(threeStudents, new StudentListEquality()).ToList();//差集
Console.WriteLine();
Console.WriteLine("以下是并集的结果");
bingji.ForEach(x =>
{
Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString());
});
Console.WriteLine();
Console.WriteLine("以下是交集的结果");
jiaoji.ForEach(x =>
{
Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString());
});
Console.WriteLine();
Console.WriteLine("以下是差集的结果");
chaji.ForEach(x =>
{
Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString());
});
Console.ReadKey();
}
} public class Student
{ public Student(int studentId, bool sex, String name, String address)
{ this.StudentId = studentId; this.Sex = sex; this.Name = name; this.Address = address;
} public int StudentId { get; set; } public bool Sex { get; set; } public String Name { get; set; } public String Address { get; set; }
} public class StudentListEquality : IEqualityComparer
{ public bool Equals(Student x, Student y)
{ return x.StudentId == y.StudentId;
} public int GetHashCode(Student obj)
{ if (obj == null)
{ return 0;
} else
{ return obj.ToString().GetHashCode();
}
}
}
}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 https://msdn.microsoft.com/zh-cn/library/bb336390(v=vs.110).aspx
运行结果如图:转载地址:https://blog.csdn.net/WuLex/article/details/80588083



