Linq Concat Union Intersect 區別

來源:互聯網
上載者:User

Concat  :連串連兩個序列。 http://msdn.microsoft.com/zh-cn/library/bb351755(v=vs.90).aspx

Union  : 通過使用預設的相等比較子產生兩個序列的並集。http://msdn.microsoft.com/zh-cn/library/bb341731(v=vs.90).aspx

Intersect : 通過使用預設的相等比較子對值進行比較產生兩個序列的交集。http://msdn.microsoft.com/zh-cn/library/bb460136(v=vs.90).aspx

通俗一點,可以這樣理解:

假設有兩序列:

var A=new Listint>{1,1,2,3,4};
var B=new Listint>{4,5,5,6,7};

那麼A.Concat(B) 表示將A序列和B序列串聯起來,以建立新的序列,不去除重複部分;

A.Union(B)表示將A序列和B序列串聯起來,並去除重複部分,以建立新的序列;

而A.Intersect(B) 只取A序列和B序列相同的部分(交集),以建立新的序列。

範例程式碼:

void Main()
{
    var A=new Listint>{1,1,2,3,4};
    var B=new Listint>{4,5,5,6,7};
    
    Print(A.Concat(B).ToList());
    Print(A.Union(B).ToList());
    Print(A.Intersect(B).ToList());
    
    /*
    ********A.Concat(B)************
    1
    1
    2
    3
    4
    4
    5
    5
    6
    7
    *********************************
    
    *********A.Union(B)***********
    1
    2
    3
    4
    5
    6
    7
    *********************************
    
    *********A.Intersect(B)***********
    4
    *********************************
    */
}
void Print(Listint> list)
{
  list.ForEach(l=>Console.WriteLine(l));
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.