【问题标题】:How to add List<Dictionary<string, byte[]> object to Dictionary<string, byte[]> [duplicate]如何将 List<Dictionary<string, byte[]> 对象添加到 Dictionary<string, byte[]> [重复]
【发布时间】:2019-01-29 11:30:49
【问题描述】:

如何将List&lt;Dictionary&lt;string, byte[]&gt; 对象添加到Dictionary&lt;string, byte[]&gt;

public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
    Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();

    foreach (var item in _commonFileCollection)
    {
        _dickeyValuePairs.add(item.key,item.value); // i want this but I am getting
        //_dickeyValuePairs.Add(item.Keys, item.Values); so I am not able to add it dictionary local variable _dickeyValuePairs 
    }
}

在 foreach 循环中,我得到了 item.KEYSitem.VALUES,所以我该如何添加它 _dickeyValuePairs

【问题讨论】:

  • 当然你需要一个嵌套的foreach - item 在你的当前循环中是一个Dictionary&lt;string, byte[]&gt;
  • _commonFileCollection 中的每个item 本身似乎都是一个字典。如果您想使用该字典 within 的项目,看起来您需要一个内部循环。

标签: c# generics generic-collections


【解决方案1】:

如果你想合并它们,那么像这样:

public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
    var _dickeyValuePairs = _commonFileCollection.SelectMany(x=> x).ToDictionary(x=> x.Key, x=> x.Value);
}

但请注意,如果它们包含相同的键 - 你会得到异常。

为了避免它 - 您可以使用查找(基本上是字典,但在值中它存储集合):

public static async void PostUploadtoCloud( List<Dictionary<string, byte[]>> _commonFileCollection)
{
    var _dickeyValuePairs = _commonFileCollection.SelectMany(x=> x).ToLookup(x=> x.Key, x=> x.Value); //ILookup<string, IEnumerable<byte[]>>
}

【讨论】:

  • 请注意,如果它们包含相同的键 - 你会得到异常。 - 有什么建议可以避免这种异常吗?
【解决方案2】:

尝试如下修改循环:

public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
{
    Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
    Byte[] itemData;
    foreach (var item in _commonFileCollection)
    {    
        foreach (var kvp in item)
        {
            if (!_dickeyValuePairs.TryGetValue(kvp.Key, out itemData))
            {
                _dickeyValuePairs.Add(kvp.Key, kvp.Value);
            }
        }

    }
}

更新:

外循环将遍历列表中的每个字典,而内循环将遍历字典的每个项目。附加部分_dickeyValuePairs.TryGetValue 将帮助您避免添加重复键时出现异常(如果有)。

【讨论】:

    【解决方案3】:

    在执行此操作时,您需要在代码中采用一些安全措施,像 @Pritish 所说的简单合并由于可能的异常而无法工作,

    public static async void PostUploadtoCloud(List<Dictionary<string, byte[]>> _commonFileCollection)
    {
     Dictionary<string, Byte[]> _dickeyValuePairs = new Dictionary<string, byte[]>();
    try
    {
     foreach (var item in _commonFileCollection)
      {
        foreach (var kvp in item)
        {
            //you can also use TryAdd
            if(!_dickeyValuePairs.Contains(kvp.Key))
            {
                _dickeyValuePairs.Add(kvp.Key, kvp.Value);
            }
            else
            {
                //send message that it could not be done?
            }
        }
    
       }
     }
     catch(Exception e)
     {
      //log exception
     } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多