1

I have 2 different lists:

public class Foo
{
    public Int32 Id { get; set; }
}

ICollection<Foo> OriginalCollection
ICollection<Foo> NewCollection

I want to generate 2 lists to be able to foreach them...

  • All items that OriginalCollection contains that NewCollection does not (basically, a collection of stuff that was removed).
  • All items that OriginalCollection does not contain that NewCollection does (basically, a collection of stuff that was added).

I know I'll get back an IEnumerable with LINQ, that's fine since I need to foreach. I just have no idea what my queries should look like...


Update:

I forgot to mention that I did try the Except clause... it failed because the objects are not the same. They only contain the same Id.

michael
  • 14,844
  • 28
  • 89
  • 177

4 Answers4

1

So, you want the Complement of the Set. This post seems to fit your needs:

Quickest way to find the complement of two collections in C#

Or perhaps the Enumerable.Except method:

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.except.aspx

Community
  • 1
  • 1
mtazva
  • 1,005
  • 9
  • 13
  • I updated my question b/c I forgot to mention that I did try the `Except` but it failed because the objects are not the same, they only contains the same Id. – michael Jun 17 '11 at 20:13
  • Did you check out my first link? It describes how to specify your own custom EualityComparer so that Except will work the way you need. – mtazva Jun 17 '11 at 20:20
  • so... link #1 is duplicate of my question. Awesome. – michael Jun 17 '11 at 20:45
0
using System.Linq;

originalC.Except(newC, comparer);
newC.Except(originalC, comparer);

Following edit:

You can use the overload that takes a "IEqualityComparer comparer".

You can define a small class that implements that interface with any kind of compare logic you wish.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111
  • I updated my question b/c I forgot to mention that I did try the `Except` but it failed because the objects are not the same, they only contains the same Id. – michael Jun 17 '11 at 20:13
0

You are looking for the Except operator

http://msdn.microsoft.com/en-us/vcsharp/aa336761.aspx#except1

NewCollection -except> OriginalCollection = deleted

OriginalCollection -except> NewCollection = added

CraigW
  • 715
  • 4
  • 5
  • I updated my question b/c I forgot to mention that I did try the `Except` but it failed because the objects are not the same, they only contains the same Id. – michael Jun 17 '11 at 20:14
0
List<Foo> original = ...
List<Foo> modified = ...
...
var deleted = original.Except(modified);
var added = modified.Except(original);

You could explicitly use LINQ but its not really worth it. One way could be:

var deleted = from i in original
              where !modified.Contains(i)
              select i;

var added = from i in modified
            where !original.Contains(i)
            select i;
InBetween
  • 32,319
  • 3
  • 50
  • 90
  • I updated my question b/c I forgot to mention that I did try the `Except` but it failed because the objects are not the same, they only contains the same Id. – michael Jun 17 '11 at 20:13