C# 兩個List泛型用 Linq去重複資料 或者 得到重複資料

來源:互聯網
上載者:User

標籤:

1, 兩個List泛型用Linq去重複資料

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,找重複資料 把linq不等號去掉就行了。

            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沒有輸出。

C# 兩個List泛型用 Linq去重複資料 或者 得到重複資料

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.