How do I use the \"NOT IN\" to get the missing data, to be added to \"foo\" List.
var accessories = new List();
var foo = new List()
You can combine Concat
and Distinct
to do this:
foo = foo.Concat(accessories).Distinct().ToList();
Edit: Or Except
as others have pointed out, which seems to be the superior choice for this case.
If you just want foo to be the distinct combination of all elements in foo and accessories (i.e., the union of the two lists),
List<string> foo = foo.Union(accessories).ToList();
There's a LINQ method to do this for you, it looks like this; accessories.Except(foo);
You can use .Except() to get the difference between two sets:
var difference = accessories.Except(foo);
// difference is now a collection containing elements in accessories that are not in foo
If you then want to add those items to foo
:
foo = foo.Concat(difference).ToList();
Use List.Except:
foo.AddRange(accessories.Except(foo));
From MSDN:
Except Produces the set difference of two sequences.