I have a POCO entity that has a hashset as navigation property. In some cases I need to add to this collection new elements, so I was thinking in something like that:
MyType myPocoEntity = new MyType();
Parallel.ForEach(myList,
(iterator, state) =>
{
myPocoEntity.MyCollection.Add(new MyType(){ Id = iterator.Id });
}
The idea is for each element in my list, create a new object that I have to add to the hashset of my POCO entity.
Just is an add operation, but I don't know if although it is only an add operation, if I could can problems, or if I should use first a concurrent collection and later pass the elements to the navigation property, but in this case I guess it would be better use a for loop and not parallel.
So I would like to know if it is a good idea to use a parallel foreach or not.
Thanks.