Simplest way to form a union of two lists

前端 未结 5 1394
再見小時候
再見小時候 2020-12-08 03:56

What is the easiest way to compare the elements of two lists say A and B with one another, and add the elements which are present in B to A only if they are not present in A

5条回答
  •  失恋的感觉
    2020-12-08 04:23

    If it is two IEnumerable lists you can't use AddRange, but you can use Concat.

    IEnumerable first = new List{1,1,2,3,5};
    IEnumerable second = new List{8,13,21,34,55};
    
    var allItems = first.Concat(second);
    // 1,1,2,3,5,8,13,21,34,55
    

提交回复
热议问题