Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing List to List with a Twist

Tags:

c#

I want to compare two List of different lengths and/or same length. The first list is considered the Original list. The 2nd list is the Updated list. The second list may be the same but ordered differently.

I would like to keep track of additions and subtractions from the list. If something was removed it could be added to a removed collection and vice versa.

Below is an example of each list with an input

Original List    Updated List
=============    ============
Jim              Tom
Tom              Mike
Mike             Harry
Sue              Sue
Kim              Roger
Roger

This is an desired output:

Addition List    Subtraction List
=============    ================
Harry            Jim
                 Kim

If you have Ideas I would be glad to hear them!

like image 298
rreeves Avatar asked Feb 22 '23 17:02

rreeves


1 Answers

Use LINQ.

var diff1 = List1.Except(List2);
var diff2 = List2.Except(List1);
like image 150
leppie Avatar answered Feb 27 '23 08:02

leppie