I have an example class
public class Item
{
public int Id;
public string Name;
public int ItemParentId;
}
then I put many Items into the database, there they have an Id, Name and ItemParentId, but I also create a list of new Items, where they have Name, ItemParentId, but Id = 0;
I do select all items from database to list1. I create new list2 with new Items.
I want to make something like this:
list1.Union(list2); // need to combine only with different ItemParentId
but the problem is that I need to combine only those items, which ItemParentId are not equal. Linq Union only let to create IEqualityComparer, but this one is not suitable. Also I tried IComparer, but Union doesn't let use it. Any help would be appreciated.
Example of lists and what result I want:
var list1 = {
Item { Id = 1, Name = "item1", ItemParentId = 100 },
Item { Id = 2, Name = "item2", ItemParentId = 200 },
Item { Id = 3, Name = "item3", ItemParentId = 300 },
Item { Id = 4, Name = "item4", ItemParentId = 400 }
}
var list2 = new List<Item>{
new Item { Id = 0, Name = "item5", ItemParentId = 500 },
new Item { Id = 0, Name = "item6", ItemParentId = 300 },
new Item { Id = 0, Name = "item7", ItemParentId = 400 },
}
result list should contain 3 items, which names are "item1", "item2", "item3", "item4" and "item5"
UPDATE:
thanks guys, with your help I managed to compare items by single property, but now I have to do that by two of them. Actually my lass has now 10 properties, but I have to compare only by two, the comparer looks fine, only thing I want to know is what do the HashCode used for?