C# 具有键和值列表的字典

C# 具有键和值列表的字典,c#,C#,如果\u集合中已经存在该键,我不知道如何从registeredValues列表向dictionary\u集合添加新值。我想向同一个键添加另一个来自registeredValues的对象。或者我做错了 private readonly List<ThreePropertyHolder<string, string, string>> _registeredValues = new List<ThreePropertyHolder<string, string,

如果
\u集合
中已经存在该键,我不知道如何从
registeredValues
列表向dictionary
\u集合
添加新值。我想向同一个键添加另一个来自
registeredValues
的对象。或者我做错了

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});
    }
}
private readonly List_registeredValues=new List();
专用词典_collection=新词典();
公共词典blablamMethod()
{
foreach(var重估为注册价值)
{
_collection.Add(rewages.Value2,新列表{rewages.Value3});
}
}

如果正在添加或正在更新,则需要使用不同的操作。用于查看该值是否存在,如果存在,则更新字典

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;
}
公共字典blablamMethod()
{
foreach(var重估为注册价值)
{
名单;
if(!\u collection.TryGetValue(重估.Value2,输出列表))
{
//密钥不存在,请向字典中添加新的空列表。
列表=新列表();
_收款。添加(重新估价。价值2,列表);
}
//现在,如果我们正在添加或更新,我们只需要添加到我们的列表中。
列表。添加(重估价值3);
}
退货(收款);;
}

通过检查密钥是否已存在,您可以选择是否添加或更新条目。根据你的样品,它应该是这样工作的

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;
}
专用只读列表
_registeredValues=新列表();
专用词典_collection=新建
字典();
公共词典blablamMethod()
{
foreach(var重估为注册价值)
{
如果(_collection.ContainsKey(重估价值2))
{
_集合[reValues.Value2]。添加(someOtherObject);
}
其他的
{
_集合。添加(
重估价值,
新列表{rewages.Value3}
);
}
}
退货(收款);;
}

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;
}
公共字典blablamMethod()
{
_集合=\u集合
.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=>newlist(x));
退货(收款);;
}
它的作用是:

  • 将集合拆分为键/值对
  • 将其与转换为键/值对的注册数据值连接起来
  • 键分组
  • 把它转换回字典

  • 如果将
    \u collection
    定义为
    Dictionary
    ,而不是
    Dictionary
    ,则这将稍微简单一些。它将消除强制转换到
    对象的需要,您可以只执行
    x.ToList()
    而不是
    新列表(x)

    您熟悉元组吗?@MethodMan不,不太熟悉。它是ThreePropertyHolder的内置版本,@ScottChamberlain谢谢您的提示,我一定会看一看……)另一件您可能需要注意的事情是,如果您多次调用
    blablablamMethod()
    ,您的代码将表现出不同的行为,因为您正在重用
    \u集合
    ,但是如果您不这样做,您可以使用
    \u registeredValues.ToLookup(x=>x.Value2,y=>y.Value3)
    ,这将为您提供一个
    查找
    ,这是一个只读的
    词典
    ,您可能需要查看它。这是错误的逻辑,您正在覆盖列表,他希望添加到列表中。如果您只是想总是覆盖它,那么只需执行
    \u collection[rewages.Value2]=新列表{rewages.Value3}就更便宜了
    每次,当它存在时会覆盖,当它不存在时会添加。你的更新更好,但效率仍然很低,不要同时执行
    ContainsKey
    \u collection[rewages.Value2]
    ,这会导致两次字典查找,如果你使用
    TryGetValue
    ,你可以在一次查找中完成。Master,完成了这项工作。谢谢