List之Union(),Intersect(),Except() 亦可以说是数学中的并集,交集,差集

来源:互联网 发布:动圈动铁圈铁 知乎 编辑:程序博客网 时间:2024/04/28 18:00

Union()

这个方法将会Union(并集)两个序列(集合)连接成一个新列表(集合)

方法定义是:

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first,IEnumerable<TSource> second, IEqualityComparer<TSource> comparer) 

Intersect ()

它将产生两个序列的交集.

方法定义是: 

public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)

public static IEnumerable<TSource> Intersect<TSource>(this IEnumerable<TSource> first, Enumerable<TSource> second,IEqualityComparer<TSource> comparer)  

Except ()

它是从一个集合中删除存在另一个集合中的项.两个序列产生的集合差. 英文意思是:除此之外

方法定义是: 

public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) 
public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second, IEqualityComparer<TSource> comparer)

实例代码分别如下:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Data;   
  6.  
  7.  
  8. namespace test  
  9. {  
  10.     class Program  
  11.     {  
  12.         static void Main(string[] args)  
  13.         {  
  14.             IList<Student> oneStudents = new List<Student>();  
  15.             oneStudents.Add(new Student(1,false,"小新1","徐汇"));  
  16.             oneStudents.Add(new Student(2,false,"小新2","闵行"));  
  17.             oneStudents.Add(new Student(3, false"小新3""嘉定"));  
  18.             oneStudents.Add(new Student(4, false"小新4""闸北"));  
  19.  
  20.             IList<Student> twoStudents = new List<Student>();  
  21.             twoStudents.Add(new Student(5, false"小新5""贵州"));  
  22.             twoStudents.Add(new Student(6, false"小新6""湖北"));  
  23.             twoStudents.Add(new Student(7, false"小新7""山东"));  
  24.             twoStudents.Add(new Student(8, false"小新8""西藏"));  
  25.  
  26.             IList<Student> threeStudents = new List<Student>();  
  27.             threeStudents.Add(new Student(1, false"小新1""徐汇"));  
  28.             threeStudents.Add(new Student(2, false"小新2""闵行"));  
  29.             var bingji = oneStudents.Union(twoStudents, new StudentListEquality()).ToList();//并(全)集   
  30.               var jiaoji = oneStudents.Intersect(threeStudents, new StudentListEquality()).ToList();//交集   
  31.               var chaji = oneStudents.Except(threeStudents, new StudentListEquality()).ToList();//差集  
  32.  
  33.               Console.WriteLine();  
  34.             Console.WriteLine("以下是并集的结果");              
  35.             bingji.ForEach(x =>  
  36.             {  
  37.                 Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString()+" "+x.Address.ToString());  
  38.        
  39.             });  
  40.             Console.WriteLine();  
  41.             Console.WriteLine("以下是交集的结果");             
  42.             jiaoji.ForEach(x =>  
  43.             {  
  44.                 Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());  
  45.  
  46.             });  
  47.  
  48.             Console.WriteLine();  
  49.             Console.WriteLine("以下是差集的结果");              
  50.             chaji.ForEach(x =>  
  51.             {  
  52.                 Console.WriteLine(x.StudentId.ToString() + "    " + x.Sex.ToString() + "   " + x.Name.ToString() + " " + x.Address.ToString());  
  53.  
  54.             });  
  55.         }  
  56.  
  57.     }  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.     public class Student  
  64.     {  
  65.         public Student(int studentId, bool sex, String name, String address)  
  66.         {  
  67.             this.StudentId = studentId;  
  68.             this.Sex = sex;  
  69.             this.Name = name;  
  70.             this.Address = address;  
  71.         }  
  72.         public int StudentId { getset; }  
  73.         public bool Sex { getset; }  
  74.         public String Name { getset; }  
  75.         public String Address { getset; }  
  76.          
  77.     }  
  78.     public class StudentListEquality : IEqualityComparer<Student>  
  79.     {  
  80.         public bool Equals(Student x, Student y)  
  81.         {  
  82.             return x.StudentId == y.StudentId;  
  83.         }  
  84.  
  85.         public int GetHashCode(Student obj)  
  86.         {  
  87.             if (obj == null)  
  88.             {  
  89.                 return 0;  
  90.             }  
  91.             else 
  92.             {  
  93.                 return obj.ToString().GetHashCode();  
  94.             }  
  95.         }  
  96.     }  
  97.  
  98.  
  99.  

以上运行的结果是:

以上的结果是重载了含有参数的IEqualityComparer<TSource> 方法,实现IEqualityComparer接口  对数据进行了重复过滤,如果不实现这个方法结果是

 var bingji = oneStudents.Union(twoStudents).ToList();//并(全)集 
 var jiaoji = oneStudents.Intersect(threeStudents).ToList();//交集 
var chaji = oneStudents.Except(threeStudents).ToList();//差集

但是对于List<T>的T是简单类型,如int  string  long 。。。。。是怎么样的呢?代码如下所示
 

  1. IList<int> firstNumbers = new List<int>()   
  2.    
  3.              {   
  4. ,2,3,4,5,6,7   
  5.  
  6.              };  
  7.  
  8.             IList<int> secondNumbers = new List<int>()   
  9.    
  10.              {   
  11. ,9,10   
  12.  
  13.              };  
  14.  
  15.             IList<int> thressNumbers = new List<int>()   
  16.    
  17.              {   
  18. ,2,3   
  19.  
  20.              };  
  21.  
  22.  
  23.             var result1 = firstNumbers.Union(secondNumbers).ToList();  
  24.             var result2 = firstNumbers.Intersect(thressNumbers).ToList();  
  25.             var result3 = firstNumbers.Except(thressNumbers).ToList();  
  26.             Console.WriteLine("以下是并集的结果");  
  27.             result1.ForEach(x => Console.WriteLine(x));  
  28.  
  29.             Console.WriteLine();  
  30.             Console.WriteLine("以下是交集的结果");  
  31.             result2.ForEach(x => Console.WriteLine(x));  
  32.  
  33.             Console.WriteLine();  
  34.             Console.WriteLine("以下是差集的结果");  
  35.             result3.ForEach(x => Console.WriteLine(x));  
  36.  
  37.             Console.WriteLine("以上是简单类型如:int string long。。。。。没有实现IEqualityComparer<T>接口"); 

结果是:

 

说明一下 刚回来看了下书,是差集 不是补集、 已更改!

0 0