var commerceMediaToRemove=writableCommerceMedia.FirstOrDefault(x => x.AssetLink.Equals(contentLink));
writableCommerceMedia.FirstOrDefault(x => x.AssetLink.Equals(contentLink));
writableCommerceMedia.Remove(commerceMediaToRemove); // this returns falseDoes anyone have any idea of what im doing wrong?
writableCommerceMedia.Remove(commerceMediaToRemove); // this returns false
It looks like you clone the CommerceMediaCollection, which is not correct. You should clone the content itself:
writable = content.CreateWritableClone() as FashionContent;writable.CommerceMediaCollection.Remove(...);
I tried that also, with the same result. It won't allow me to remove the media.
Can you post the full snippet here? Did you make sure commerceMediaToRemove is not null?
var clone = currentContent.CreateWritableClone<EntryContentBase>();
var itemToRemove = clone.CommerceMediaCollection.FirstOrDefault(x => x.AssetLink.Equals(contentLink));
contentRepo.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);We had a product in the CommerceMediaCollection, i'm not sure how it got there. After i deleted the product from the db table CatalogItemAsset, the code run successfully. The item was found and .Remove() returned true, however i would want to be able to get it right without going in to the database and manually delete the row.
contentRepo.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
Yes, im sure the commerceMediaToRemove was not null.
It just does not make senses,
is just some standard collection modification, it returns false if the collection does not contain itemToRemove. Can you debug to be sure: which are in the collection, what is in the itemToRemove?
The only reasonable explaination I can come up with is itemToRemove is null, and .Remove will return false because it relies on .Contains to check https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.contains?view=netframework-4.7.2
the underlying implementation of CommerceMediaCollection is List<T>
I did debugg the code several times and the itemToRemove was not null.The CommerceMediaCollection contains 7 items including the itemToRemove item which was at index 0.
Then I'd suggest you to contact our developer support service for further assistance
What if you clear the collection and re-add items you want to keep?
Okej, thanks Quan.
Maris, never thought of that. I will give it a try.