-1

I have two lists: list1 and list2. I want to join the lists in a way that the values in list1 should not be affected, even if it contains duplicate values. However, when appending values from list2 the duplicate values should not be removed.

I had tried Union, which will distinct all the values and eliminate my repeated values in list1.

For example

list1 = [a, b, c, c, d, e] 
list2 = [a,c,f,g] 
my required solution 
list3=[a, b, c, c, d, e, f, g] 
oleksii
  • 35,458
  • 16
  • 93
  • 163
niXy
  • 11
  • 2
  • Can you post the relevant code so everyone can have a look at where you're going wrong – Izzy Sep 12 '14 at 10:14
  • [`.Concat()`](http://msdn.microsoft.com/ru-ru/library/bb302894(v=vs.110).aspx) – Artyom Neustroev Sep 12 '14 at 10:15
  • http://stackoverflow.com/questions/1528171/joining-two-lists-together Can you clarify your question? I can't seem to pick it up. – Irvin Denzel Torcuato Sep 12 '14 at 10:15
  • for example list1 = [a, b, c, c, d, e] list2 = [a,c,f,g] my required solution list3=[a, b, c, c, d, e, f, g] – niXy Sep 12 '14 at 10:17
  • @ArtyomNeustroev Concat will just join two list. I have Specific requirement – niXy Sep 12 '14 at 10:21
  • It really helps if you just try to think of a way to do that on paper or just describe the algorithm, e.g. *I start with copying the first list. Then for each item in the second list I do ....*. – Dirk Sep 12 '14 at 10:22

3 Answers3

1
var list3 = list1.ToList();
foreach (var val in list2)
{
    if (!list3.Contains(val))
        list3.Add(val);
}

This would give you a new list with all the values from list1 and list2.

Mr Jones
  • 134
  • 7
1
var MyList = List1.Concat(List2.Except(List1).ToList());
David Pilkington
  • 13,528
  • 3
  • 41
  • 73
0

You would want to use something like this

var list1 = new List<int> {1, 2, 3, 3}; 
var list2 = new List<int> {1, 2, 3, 4, 5, 3};
var list3 = list1.Concat(list2.Except(list1)); // 1, 2, 3, 3, 4, 5
oleksii
  • 35,458
  • 16
  • 93
  • 163