C# Linq獲取兩個List或數組的差集交集

List< int> list1 = new List<int>(); list1.Add(1); list1.Add(2); list1.Add(3); List<int> list2 = new List<int>(); list2.Add(3); list2.Add(4); list2.Add(5); //獲得的結果是4,5 即減去了相同的元素。 List<int> list3 = list2.Except(list1).ToList(); foreach (int i in list3) {     MessageBox.Show(i.ToString()); }
複製代碼
合併兩個數組,並去掉重複元素,而後排序(C#)
複製代碼
List< int> numbers1 = new List<int>() { 5, 4, 1, 3, 9, 8, 6, 7, 12, 10 }; List<int> numbers2 = new List<int>() { 15, 14, 11, 13, 19, 18, 16, 17, 12, 10 }; var newQuerty = numbers1.Concat( from n in numbers2 where !numbers1.Contains(n) select n ).OrderBy(n=>n);
複製代碼
合併兩個數組,並去除合併後的重複數據, 並排序

            int[] A={1,2,2,3,4,5,6,6,6};             int[] B={2,2,2,3,7,8,9,5};數組

            List<int> list = new List<int>(A);             list.AddRange(B);函數

            list.Sort();ui

            //去除重複項             foreach (int i in list.Distinct<int>())             {                 Console.WriteLine(i);             }this

C# 取兩個數組的相同元素

以往咱們都是確定絞盡腦汁,確定什麼循環,元素大小,什麼因素都考慮進去。可是如今採用Linq能夠很好的解決這個問題。找出兩個或多個數組的相同項。google

代碼至關簡單:spa

usingSystem;code

usingSystem.Collections.Generic;orm

usingSystem.Linq;blog

usingSystem.Text;排序

 

namespaceTest4_03

{

   classProgram

    {

       staticvoidMain(string[] args)

        {

           string[] names = {"Adams","Arthur","Buchanan","Tsbuchis","ShCian","FuchsiaLinda","DecheChen","Lotheer","FindLanciCade","SorchLand","JiangZheng","MisiiLoda","Gtod","Dfac","Lama","BakCades","Losangle","ZheWQ","GehengDahaLothi","ToryLandey","DakaLothy","BthLanda","MenNorth","Fith","FoxMain","DontM","Saobba","Del","Sala","Ghero","BhthLaPhda"};

           IEnumerable<string> skip = names.Skip(10);

           IEnumerable<string> take = names.Take(11);

 

           //取出兩個序列中交集部分,按理論應該輸出JiangZheng

           IEnumerable<string> intersect = skip.Intersect(take);

 

           foreach(varsinintersect)

            {

               Console.WriteLine(s);

            }

           Console.ReadKey();

        }

    }

}

 

C# 獲取兩個數組集合的差集,交集

今天在作一個樹形選擇節點時,遇到一個問題,屬性節點是記錄了相關的ID值,第一次呢所有對這些ID進行處理,可是接下來再次選擇就要分狀況了,原先選擇的ID若是不在新選擇的集合中那麼剔除掉,不然,原先ID不傳入函數處理,新ID傳入函數處理:

好比原來①選擇的ID是:1,2,3,4                下次:1,2,3,4,5,   那麼這時候5要處理,1,2,3,4維持原樣。

              ②選擇ID是:1,3                          下次: 3,4,5           那麼這時候4,5 要處理,3 維持原樣。1剔除。

              ③選擇ID是:1,2,3,4,5                  下次:3,4,5             那麼這時候3,4,5都維持原樣,1,2剔除。

              ④選擇ID是:1,2                           下次:3,4,5             那麼這時候3,4,5處理,1,2剔除。

 

簡化一下數學模型:

你們發現沒其實這就是一個數學的概念,集合的差集,那麼咱們怎麼處理呢? 假設前次選擇的集合爲A,後次選擇爲B

獲得要處理的很簡單:B-A (B與A的差集)就是要處理的集合元素,爲何呢?根據概念可知哈!

那麼獲得不作處理的怎麼辦呢? 不要處理的必然是B的子集,那麼怎麼獲得呢?

出來啦既是:B-(B-A)    這是爲何呢? B-A  就是要處理的,而維持原樣的就是固然就是:B-(B-A),

那麼剔除的集合呢? A-(B-(B-A)) 

 

如何用C#表示呢,我這裏就不用什麼循環之類的了,我用的是NET3.5 那就好辦了,用Linq處理:

俺這裏特殊點,右鍵獲得的樹形集合(lstSource)包含了其餘信息,先獲取ID集合再說:

 var m_ilAllSelect = lstSource.Select(r => r.ID).AsEnumerable();//新選擇的列表

///////下面開始處理了

List<int> m_ilNewSelect = m_ilAllSelect.ToList();//新選擇列表 List<int> m_ilExcept = m_ilNewSelect.Except(m_mcuids).ToList(); //二者的不一樣之處 List<int> m_iExceptAfterAndNew = m_ilNewSelect.Except(m_ilExcept).ToList();//新選擇列表與差集比較,則是新選擇中的舊的

 

爲了簡化給你們,這裏的A表明舊集合,B表明新集合,這裏的集合都是List<int>泛型列表。

那麼要處理的就是 B.Except(A), 維持原樣的:B( B.Except(A)), 剔除的:A.Except(B( B.Except(A))),

不要問我這個Except方法啥意思?看MSDN吧,google也行啦!

相關文章
相關標籤/搜索