Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# comparing collections

Tags:

c#

collections

I'm new to C# and need some help with comparing collections. I have two List<string> collections with their contents as below:

Collection Old: {"AAA","BBB","CCC"}

Collection New: {"BBB","CCC","DDD"}

I want to get a collection like below:

Collection Final: {"AAA", "Remove"; "BBB", "Keep"; "CCC", "Keep"; "DDD", "Add"}

How can I do this?

like image 585
K.R. Avatar asked Dec 20 '25 05:12

K.R.


1 Answers

old.Except(new) will give you those items to remove

new.Except(old) will give you items to add

old.Intersect(new) will give you items to keep

(This is assuming you don't mind using the System.Linq namespace)

Or if you prefer, you can consider each item individually and check the existence in each list

like image 99
Kieren Johnstone Avatar answered Dec 22 '25 18:12

Kieren Johnstone