Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complement of Two Lists?

lets say I have a list of strings:

A,B,C,D

Then another list of strings

B,C,D

I want to know what elements are in the first list that aren't in the second list, so the result would be A

I don't know the name of the extension method to do this is. I know I can use concat, union, intersect for similar list comparisons, but just don't know the name to accomplish this particular task.

Addendum, I am interested in duplicates, so if the first list is:

A,A,A,B,C,D

and the second list is

B,C,D

i want to get

A,A,A

Thanks!

like image 706
sooprise Avatar asked May 09 '11 15:05

sooprise


1 Answers

You can use the Except Extension Method to get all elements in a list that are not in a second list:

var result = list1.Except(list2);
like image 124
dtb Avatar answered Oct 01 '22 19:10

dtb