Alex Rivera | Logout

Linq find differences in two lists

Asked 2010-03-08T19:59:42.827
18

I have two list of members like this:

Before: Peter, Ken, Julia, Tom

After: Peter, Robert, Julia, Tom

As you can see, Ken is is out and Robert is in.

What I want is to detect the changes. I want a list of what has changed in both lists. How can linq help me?

Edit
Report

1 Answer

29

Your question is not completely specified but I assume that you are looking for the differences as sets (that is, ordering does not matter). If so, you want the symmetric difference of the two sets. You can achieve this using Enumerable.Except:

before.Except(after).Union(after.Except(before));
answered 2010-03-08T20:01:49.627

Your Answer