1, 兩個List泛型用lamada表達式去重復數據
Code:
List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" }; List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" }; List<string> newList = lstA.FindAll(x => !lstB.Contains(x)); foreach (var item in newList) { Console.WriteLine(item); } Console.ReadKey();
控制台運行結果
圖1
圖1 為什么會出現這個情況,什么都沒有輸出。下面修改下程序,請看2
2,先看 Code:
List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" }; List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" }; List<string> newList = lstB.FindAll(x => !lstA.Contains(x)); foreach (var item in newList) { Console.WriteLine(item); } Console.ReadKey();
再看結果:
圖2
看下加粗線 的代碼 和圖2結果,想必你看出所以然了。
把 A數組 當做 A區間 B數組 當做 B區間, B區間的 范圍大於 A區間 ,把A、B兩區間的 公共部分除掉,不同部分找出來。
1 結果 沒有數據輸出,原因就在此。
3,在 A數組 在 加個 mac地址 這時候 A、B兩區間范圍一樣大
code:
List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" }; List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" }; List<string> newList = lstB.FindAll(x => !lstA.Contains(x)); foreach (var item in newList) { Console.WriteLine(item); } Console.ReadKey();
再看結果:
圖3
圖3和圖2的結果一樣,不重復的數據 應該是 E00401501B652562,E00401501B652569這兩個mac地址才正確。
這時候代碼怎么調整呢?看 4
4,Code
List<string> lstA = new List<string> { "E00401501B652563", "E00401501B652562", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" }; List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" }; List<string> listAll = new List<string>(); List<string> listResult = new List<string>(); var listUnionAll = lstA.Union(lstB).OrderBy(t => t);//排序合並數據 foreach (var item in listUnionAll) { listAll.Add(item); } List<string> newList = listAll.FindAll(x => !lstA.Contains(x));//去重復,組合新的List集合 List<string> newList2 = listAll.FindAll(x => !lstB.Contains(x)); var Unionlist = newList.Union(newList2).OrderBy(t => t);//排序合並數據 foreach (var item in Unionlist) { Console.WriteLine(item); } Console.ReadKey();
結果
5,找重復數據 把lamada表達式不等於號去掉就行了。
List<string> lstA = new List<string> { "E00401501B652562", "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568" }; List<string> lstB = new List<string> { "E00401501B652563", "E00401501B652564", "E00401501B652565", "E00401501B652566", "E00401501B652567", "E00401501B652568", "E00401501B652569" }; List<string> newList = lstB.FindAll(x => lstA.Contains(x)); foreach (var item in newList) { Console.WriteLine(item); } Console.ReadKey();
結果
mac地址 62和69沒有輸出。