Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to merge two lists and remove duplicates

Tags:

c#

join

list

linq

Given I have two list like the following:

var listA = new List<string> { "test1", "test2", "test3" };
var listB = new List<string> { "test2", "test3", "test4" };

I want a third list with:

var listC = new List<string> { "test1", "test2", "test3", "test4"}

Is there a way to get this?

like image 763
LuisOsv Avatar asked Jan 20 '17 15:01

LuisOsv


People also ask

How do you merge Excel lists and remove duplicates?

In Excel, there is no built-in function can quickly merge sheets and remove duplicates, you just can copy and paste the sheet contents one by one then apply Remove Duplicates function to remove the duplicates.


1 Answers

Try the Union extension method.

var result = listA.Union(listB).ToList();

Union produces the set union of two sequences by using the default equality comparer so the result contains only distinct values from both lists.

like image 79
Damian Avatar answered Oct 11 '22 17:10

Damian