Equality Comparison with Multiple Instances/IEqualityComparer problems in LINQ

Posted by Stacey on Stack Overflow See other posts from Stack Overflow or by Stacey
Published on 2010-05-08T02:11:48Z Indexed on 2010/05/08 2:18 UTC
Read the original article Hit count: 314

Filed under:
|

This is similar to my last question; but from a different angle. http://stackoverflow.com/questions/2792393/see-if-item-exists-once-in-enumerable-linq

Given the following set of items, and lists containing them...

Item 1
Item 2
Item 3
Item 4
Item 5

class Item
{
 string Name { get; set; }
}

List<Item> available = new List<Item>()
{
 Item 1
 Item 1
 Item 2
 Item 3
 Item 5
}

List<Item> selected = new List<Item>()
{
 Item 1
 Item 2
 Item 3
}

I need to make a third List that has everything from "available", except what is in "selected". However 'Item 1' is in 'available' twice, but only in 'selected' once. Since they are instances of the same item, I am having trouble figuring out the appropriate logic to accomodate this.

The final array should look like...

List<Item> selectable = new List<Item>()
{
 Item 1
 Item5
}

© Stack Overflow or respective owner

Related posts about LINQ

Related posts about c#