【发布时间】:2019-03-29 22:33:11
【问题描述】:
我的案子很复杂。我想更改此字典中 KeyVluePair 的值 -> Dictionary<string, List<KeyValuePair<string, int>>>
到目前为止我已经完成了,但我不知道如何继续:
string input = Console.ReadLine();
Dictionary<string, List<KeyValuePair<string, int>>> dworfs = new Dictionary<string, List<KeyValuePair<string, int>>>();
while (input != "Once upon a time")
{
string[] elements = input.Split(new[] { " <:> " }, StringSplitOptions.RemoveEmptyEntries);
if (dworfs.ContainsKey(elements[0]))
{
if (dworfs[elements[0]].Any(x => x.Key.Contains(elements[1])))
{
var dworf = dworfs[elements[0]].FirstOrDefault(x => x.Key == elements[1]);
if (dworf.Value < int.Parse(elements[2]))
{
dworfs[elements[0]].FirstOrDefault(x => x.Key == elements[1]) = new KeyValuePair<string,int> (elements[1], int.Parse(elements[2]));
}
}
else
{
dworfs[elements[0]].Add(new KeyValuePair<string, int>(elements[1], int.Parse(elements[2])));
}
}
else
{
dworfs.Add(elements[0], new List<KeyValuePair<string, int>> { new KeyValuePair<string, int> (elements[1], int.Parse(elements[2])) });
}
input = Console.ReadLine();
}
dworfs[elements[0]].FirstOrDefault(x => x.Key == elements[1]) = new KeyValuePair<string,int> (elements[1], int.Parse(elements[2])); 这一行给了我一个错误赋值的左侧必须是变量、属性或索引器。我不知道如何分配价值。有人可以帮忙吗?
【问题讨论】:
标签: c# .net list dictionary keyvaluepair