Union() 這個(gè)方法將會(huì)Union(并集)兩個(gè)序列(集合)連接成一個(gè)新列表(集合) 方法定義是: public static IEnumerable<TSource> Union<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second)
Intersect () 它將產(chǎn)生兩個(gè)序列的交集. 方法定義是: 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 () 它是從一個(gè)集合中刪除存在另一個(gè)集合中的項(xiàng).兩個(gè)序列產(chǎn)生的集合差. 英文意思是:除此之外 方法定義是: public static IEnumerable<TSource> Except<TSource>(this IEnumerable<TSource> first, IEnumerable<TSource> second) 實(shí)例代碼分別如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; namespace test { class Program { static void Main(string[] args) { IList<Student> oneStudents = new List<Student>(); 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<Student> twoStudents = new List<Student>(); 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<Student> threeStudents = new List<Student>(); 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("以下是并集的結(jié)果"); bingji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString()+" "+x.Address.ToString()); }); Console.WriteLine(); Console.WriteLine("以下是交集的結(jié)果"); jiaoji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); Console.WriteLine(); Console.WriteLine("以下是差集的結(jié)果"); chaji.ForEach(x => { Console.WriteLine(x.StudentId.ToString() + " " + x.Sex.ToString() + " " + x.Name.ToString() + " " + x.Address.ToString()); }); } }
以上運(yùn)行的結(jié)果是: 以上的結(jié)果是重載了含有參數(shù)的IEqualityComparer<TSource> 方法,實(shí)現(xiàn)IEqualityComparer接口 對(duì)數(shù)據(jù)進(jìn)行了重復(fù)過(guò)濾,如果不實(shí)現(xiàn)這個(gè)方法結(jié)果是 var bingji = oneStudents.Union(twoStudents).ToList();//并(全)集 但是對(duì)于List<T>的T是簡(jiǎn)單類型,如int string long 。。。。。是怎么樣的呢?代碼如下所示 IList<int> firstNumbers = new List<int>() { 1,2,3,4,5,6,7 }; IList<int> secondNumbers = new List<int>() { 8,9,10 }; IList<int> thressNumbers = new List<int>() { 1,2,3 }; var result1 = firstNumbers.Union(secondNumbers).ToList(); var result2 = firstNumbers.Intersect(thressNumbers).ToList(); var result3 = firstNumbers.Except(thressNumbers).ToList(); Console.WriteLine("以下是并集的結(jié)果"); result1.ForEach(x => Console.WriteLine(x)); Console.WriteLine(); Console.WriteLine("以下是交集的結(jié)果"); result2.ForEach(x => Console.WriteLine(x)); 結(jié)果是:
說(shuō)明一下 剛回來(lái)看了下書(shū),是差集 不是補(bǔ)集、 已更改! |
|
來(lái)自: 昵稱10504424 > 《C#》