我正在尝试按照以下方式更改字典中键的值:
//This part loads the data in the iterator
List<Recommendations> iterator = LoadBooks().ToList();
//This part adds the data to a list
List<Recommendations> list = new List<Recommendations>();
foreach (var item in iterator.Take(100))
{
list.Add(item);
}
//This part adds Key and List as key pair value to the Dictionary
if (!SuggestedDictionary.ContainsKey(bkName))
{
SuggestedDictionary.Add(bkName, list);
}
//This part loops over the dictionary contents
for (int i = 0; i < 10; i++)
{
foreach (var entry in SuggestedDictionary)
{
rec.Add(new Recommendations() { bookName = entry.Key, Rate = CalculateScore(bkName, entry.Key) });
entry.Key = entry.Value[i];
}
}
但是代码提示说"属性或索引器 KeyValuePair>.Key 无法赋值,因为它是只读的。" 我想要做的是在这里更改字典 Key 的值并将其分配给另一个值。
SuggestedDictionary
的类型参数是什么? - dtanabeTentity
赋值给一个string
呢?或者实际上是Dictionary<string, List<string>>
吗?如果你想要将现有的键值映射重新关联到一个新的键上,那么 @TheGeneral 发布的答案是准确的。 - dtanabe