I have a dictionary of which I want to modify the key by removing the previous one and adding a new one and then Iterating over it again and again. Here is the declaration of dictionary
Dictionary<string, List<Entity>> SuggestedDictionary = new Dictionary<string, List<Entity>>
And: The other dictionary is :
Dictionary<string, List<Entity>> CopyDataDict = new Dictionary<string, List<Entity>>
After that I populate data in the dictionary using Dict.Add().
List<Entity> list = db.books.ToList();
foreach(var l in list)
{
list.Add(l);
}
SuggestedDictionary.Add("Key", list);
CopyDataDict.Add("Key", list);
Then I iterate over the data as follows:
foreach (var entry in CopyDataDict.Values.ToList())
{
for (int i = 2; i < 15; i++) //just 14 items will be added
{
foreach (var container in SuggestedDictionary.Keys)
{
rec.Add(new Recommendations() { bookName = container, Rate = CalculatePearsonCorrelation(bkName, container) });
}
SuggestedDictionary.Remove(SuggestedDictionary.Keys.ToString());
if (!SuggestedDictionary.ContainsKey(entry[i].bookName))
{
SuggestedDictionary.Add(entry[i].bookName, list);
}
}
When I run the code, it says The Collection has been modified enumeration operator may not execute. How Can I fix it or is there a better solution to do the same thing.