I'm trying to delete one or more items from an entity collection.
The code I use is this
private RelatedProductCollection collection; // A collection class, generated by LLBLgen
public void removeProduct(int productID)
{
IPredicateExpression selectFilter = new PredicateExpression(RelatedProductFields.ProductId == productID);
List<int> matches = collection.FindMatches(selectFilter);
foreach (int i in matches)
{
collection[i].Delete();
}
}
The problem that I find is that this will work once, but the second time I call the function I get an exception at the line
List<int> matches = collection.FindMatches(selectFilter);
with the message
ORMEntityIsDeletedException
{"This entity is deleted from the database and can't be used in logic."}
I guess I'm not clear about what is deleted. Why wouldn't the FindMatches method work on a collection that is still in existence? (Even if some entries have been deleted)
What would be the correct way to implement such a delete?
Thanks for any help.
Robert.