[英]Dictionary with key and value list
I can't figure out how to add to dictionary _collection
new value from registeredValues
list if the key already exists in _collection
. 如果密钥已存在于_collection
我无法弄清楚如何从registeredValues
列表中添加到字典_collection
新值。 I want to add to the same key another object from registeredValues
. 我想从registeredValues
向同一个键添加另一个对象。 Or maybe I'm doing it wrong? 或者也许我做错了?
private readonly List<ThreePropertyHolder<string, string, string>> _registeredValues = new List<ThreePropertyHolder<string, string, string>>();
private Dictionary<string, List<object>> _collection = new Dictionary<string, List<object>>();
public Dictionary<string, List<object>> BlaBlaMethod()
{
foreach (var reValues in _registeredValues)
{
_collection.Add(reValues.Value2, new List<object>{reValues.Value3});
}
}
You need to have a different action if it is adding or it is updating. 如果要添加或正在更新,则需要执行不同的操作。 Use TryGetValue
to see if the value exists and update the dictionary if it does. 使用TryGetValue
查看值是否存在并更新字典(如果存在)。
public Dictionary<string, List<object>> BlaBlaMethod()
{
foreach (var reValues in _registeredValues)
{
List<object> list;
if(!_collection.TryGetValue(reValues.Value2, out list))
{
//The key was not there, add a new empty list to the dictionary.
list = new List<object>();
_collection.Add(reValues.Value2, list);
}
//Now, if we are adding or updating, we just need to add on to our list.
list.Add(reValues.Value3);
}
return _collection;
}
A LINQ version in case anyone's interested: LINQ版本以防任何人感兴趣:
public Dictionary<string, List<object>> BlaBlaMethod()
{
_collection = _collection
.SelectMany(x => x.Value, (x, y) => new { x.Key, Value = y })
.Concat(_registeredValues.Select(x => new { Key = x.Value2, Value = (object)x.Value3 }))
.GroupBy(x => x.Key, x => x.Value)
.ToDictionary(x => x.Key, x => new List<object>(x));
return _collection;
}
What this does: 这是做什么的:
_collection
into key/value pairs. 将_collection
转换为键/值对。 _registeredValues
converted into key/value pairs. 将_registeredValues
转换为键/值对进行连接。 Key
. Key
分组。 This would be a bit simpler if _collection
were defined as Dictionary<string, List<string>>
rather than Dictionary<string, List<object>>
. 如果将_collection
定义为Dictionary<string, List<string>>
而不是Dictionary<string, List<object>>
这会更简单_collection
。 It would eliminate the need to cast to object
, and you could just do x.ToList()
instead of new List<object>(x)
. 它将消除转换为object
的需要,您可以只执行x.ToList()
而不是new List<object>(x)
。
By checking if the key already exists you can chose whether to add or updated an entry. 通过检查密钥是否已存在,您可以选择是添加还是更新条目。 Based on your sample it should work like this. 根据您的样本,它应该像这样工作。
private readonly List<ThreePropertyHolder<string, string, string>>
_registeredValues = new List<ThreePropertyHolder<string, string, string>>();
private Dictionary<string, List<object>> _collection = new
Dictionary<string, List<object>>();
public Dictionary<string, List<object>> BlaBlaMethod()
{
foreach (var reValues in _registeredValues)
{
if (_collection.ContainsKey(reValues.Value2))
{
_collection[reValues.Value2].Add(someOtherObject);
}
else
{
_collection.Add(
reValues.Value2,
new List<object> { reValues.Value3 }
);
}
}
return _collection;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.