I have a collection of type:
Iesi.Collections.Generic
public ISet<ItemBinding> ItemBindings { get; set; }
where ItemBinding
is Domain.Model
I initialize the collection in this way:
ItemBindings = new HashedSet<ItemBinding>();
and I fill the collection with members.
When i want to remove an item from this collection i can't remove it.
private void OnRemove(ItemBinding itemToRemove) {
ItemBindings.Remove(itemToRemove);
}
even the itemToRemove
has the same hashCode
as the item from the collection.
Also I tried in to find the item in collection, keep it in a variable, and remove it:
private void OnRemove(ItemBinding itemToRemove) {
var foundItem = ItemBindings.Single( x => x.Id == itemToRemove.Id); // always if found
ItemBindings.Remove(foundItem);
}
but this doesn't work.
An workaround which works ok is this:
private void OnRemove(ItemBinding itemToRemove) {
var backUpItems = new List<ItemBinding>(ItemBindings);
backUpItems.Remove(itemToRemove);
ItemBindings.Clear();
ItemBindings.AddAll(backUpItems);
}
but this is an dirty workaround. I'm trying to do this simple Remove in an elegant manner :).
CHANGE the TYPE
If I change the type from ISet in IList it works ok.
public IList<ItemBinding> ItemBindings { get; set; }
ItemBindings = new List<ItemBinding>();
When i want to remove an item from this collection IT IS REMOVED.
private void OnRemove(ItemBinding itemToRemove) {
ItemBindings.Remove(itemToRemove);
}
What i'm missing in the way that i can't remove items from ISet ... ?
Thank you for suggestions, solutions.