c#提供了Intersect来得到两个列表的交集,它是通过使用默认的相等比较器对值进行比较生成两个序列的交集,定义为:
public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second);
我们使用它来比较两个列表试试:
List<double> oneList = new List<double> { 9, 8, 5, 3 }; List<double> twoList = new List<double> { 9, 3 }; List<double> result = oneList.Intersect(twoList).ToList();
结果是{9,3}
我们再来试试对象列表
我们先定义一个模型
public class TarGetValueList { public double TargetValue { get; set; } }
然后求交集
List<TarGetValueList> ListOne = new List<TarGetValueList>(); List<TarGetValueList> ListTwo= new List<TarGetValueList>(); //第一个列表增加的对象 TarGetValueList Night = new TarGetValueList() { TargetValue = 9.0 }; TarGetValueList Sex = new TarGetValueList() { TargetValue = 6.0 }; TarGetValueList Seven = new TarGetValueList() { TargetValue = 7.0 }; TarGetValueList Five = new TarGetValueList() { TargetValue = 5.0 }; //第二个列表增加的对象 TarGetValueList Three = new TarGetValueList() { TargetValue = 3.0 };//第一个列表增加对象 ListOne.Add(Night); ListOne.Add(Sex); ListOne.Add(Five); ListOne.Add(Seven);//9 6 5 7 //第二个列表增加对象 ListTwo.Add(Five); ListTwo.Add(Three); //5 3 List<TarGetValueList> resultList = ListTwo.Intersect(ListOne).ToList();
第一个列表是{9,6,5,7},第二个列表是{5,3},结果交集是{5},结果在预想之内,至今为止,一切都很顺利。我们似乎已经掌握了用c#来求两个列表的交集,但让我们在以上的例子上进行以下的尝试:
List<TarGetValueList> ListOne = new List<TarGetValueList>(); List<TarGetValueList> ListTwo= new List<TarGetValueList>(); //第一个列表增加的对象 TarGetValueList Night = new TarGetValueList() { TargetValue = 9.0 }; TarGetValueList Sex = new TarGetValueList() { TargetValue = 6.0 }; TarGetValueList Seven = new TarGetValueList() { TargetValue = 7.0 }; TarGetValueList Five = new TarGetValueList() { TargetValue = 5.0 }; //第二个列表增加的对象 TarGetValueList Three = new TarGetValueList() { TargetValue = 3.0 }; TarGetValueList FiveTwo = new TarGetValueList() { TargetValue = 5.0 }; //第一个列表增加对象 ListOne.Add(Night); ListOne.Add(Sex); ListOne.Add(Five); ListOne.Add(Seven);//9 6 5 7 //第二个列表增加对象 ListTwo.Add(FiveTwo); ListTwo.Add(Three); //5 3 List<TarGetValueList> resultList = ListTwo.Intersect(ListOne).ToList();
第一个列表仍然是{9,6,5,7},第二个列表仍然是{5,3},得到的结果交集列表却为空了。第二个列表的5跟第一个列表的5来自不同的对象,不过对象里的值都是一样的。但Intersect函数判断它们不是交集对象,这是因为判断对象是否相等是根据它们的内存地址来判断,那我们判断对象之间相等就没办法了吗?我们重写IEqualityComparer即可:
public class TarGetComparer : IEqualityComparer<TarGetValueList> { public bool Equals(TarGetValueList x, TarGetValueList y) { try { return x.TargetValue == y.TargetValue; } catch(Exception ex) { return base.Equals(x); } } public int GetHashCode(TarGetValueList obj) { return obj.TargetValue.GetHashCode(); } }
List<TarGetValueList> ListOne = new List<TarGetValueList>(); List<TarGetValueList> ListTwo= new List<TarGetValueList>(); //第一个列表增加的对象 TarGetValueList Night = new TarGetValueList() { TargetValue = 9.0 }; TarGetValueList Sex = new TarGetValueList() { TargetValue = 6.0 }; TarGetValueList Seven = new TarGetValueList() { TargetValue = 7.0 }; TarGetValueList Five = new TarGetValueList() { TargetValue = 5.0 }; //第二个列表增加的对象 TarGetValueList Three = new TarGetValueList() { TargetValue = 3.0 }; TarGetValueList FiveTwo = new TarGetValueList() { TargetValue = 5.0 }; //第一个列表增加对象 ListOne.Add(Night); ListOne.Add(Sex); ListOne.Add(Five); ListOne.Add(Seven);//9 6 5 7 //第二个列表增加对象 ListTwo.Add(FiveTwo); ListTwo.Add(Three); //5 3 List<TarGetValueList> resultList = ListTwo.Intersect(ListOne,new TarGetComparer()).ToList();
这样就得出了我们预期的结果{5}