Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get distinct list between two lists in C#

Tags:

I have two lists of strings. How do I get the list of distinct values between them or remove the second list elements from the first list?

List<string> list1 = { "see","you","live"}  List<string> list2 = { "see"} 

The result should be {"you","live"}.

like image 404
kartal Avatar asked Jul 11 '11 22:07

kartal


1 Answers

It looks to me like you need Enumerable.Except():

var differences = list1.Except(list2); 

And then you can loop through the differences:

foreach(var difference in differences) {     // work with each individual string here. } 
like image 120
Justin Niessner Avatar answered Oct 06 '22 02:10

Justin Niessner