0

How to add List<Dictionary<string, byte[]> object to Dictionary<string, byte[]>

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 
    }
}

In foreach loop I am getting item.KEYS and item.VALUES so how I can add to it _dickeyValuePairs

eocron
  • 6,885
  • 1
  • 21
  • 50
Pritish
  • 2,194
  • 2
  • 21
  • 44

3 Answers3

1

If you want to merge them, then something like this:

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

But beware that if they contain same keys - you will get exception.

To avoid it - you can use lookup (basically dictionary but in value it stores collection):

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[]>>
}
eocron
  • 6,885
  • 1
  • 21
  • 50
1

Try with modify loop like the following:

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);
            }
        }

    }
}

Update:

The outer loop will iterate through each dictionaries in the list, where as the inner loop will iterate each items of the dictionary. The additional part _dickeyValuePairs.TryGetValue will help you to avoid exception on adding duplicate keys if any.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
1

You need to employ some safety in your code when doing this, a simple merge like @Pritish said wont work due to the possible exception,

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
 } 
}
JohnChris
  • 1,360
  • 15
  • 29