I have two list list1 and list2
List1 contains
1 a
2 b
3 c
4 d
List2 contains
3 c
4 d
5 e
I want following list using LINQ
1 a
2 b
3 c
4 d
5 e
List1.Concat(List2.Where(l2 => !List1.Contains(l2))).ToList()
Since I don't see this answer here i'll post it...
The best way to remove duplicates while merging two lists together is Enumerable.Union:
var foo = new List<char> { 'a', 'b', 'c', 'd' };
var bar = new List<char> { 'c', 'd', 'e' };
var result = foo.Union(bar).ToList();
The rest of the answers work, but Linq has a built-in way to do this.
var Lst = List1.Concat(List2.Where(l2 => List1.All(x => x.Id != l2.Id))).ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With