Convert Dictionary to KeyValuePairs

Hi, I have a Dictionary (of Integer, Dictionary (of String, Datatable)).
a) Instead of using a for each loop through keyvaluepair (of Integer, Dictionary (of String, Datatable)), I need to use a do while loop where I increment the index by counter.
b) And then within the do while loop, I have a for each loop to write Dictionary (of String, Datatable).

My question is how do I write a)? This is my code right now but it is giving me an exception.


1 Like


Can you share your xaml file or a xaml with your loops?
Because I’can’t figure your dictionaries imbrications


Use the Output Index Property of For each Activity to get Iteration Index.

i am using a counter to increment the index, that part is alright.
What I’m trying to do is to not use the for each loop.

Global_WriteTableandDictionaryIntoExcel.xaml (43.0 KB)


I do not understand why you don’t want to use a For Each loop as you do in your code?
I see that the error that you have is “Key is not present”, are you sure that the int32 key of each element of “i_DictionaryofDictionaries” are perfectly sequential (0,1,2,3,4,…)? Because the loop that you put in place in your flow chart imposes it. If not, it may cause your “Key not found exception”.

To handle that you must add a condition before your “for each” loop to verify that the key “DictionaryCounter” exists in “i_DictionaryofDictionaries”. (Mind also to adjust your condition in the flowchart.

I do not know if that answers your question …

Using foreach is the simplest solution. But if your requirement requires using while loop with counter, then create a variable with type as System.Collections.Generic.KeyValuePair<TKey, TValue> by specifying the datatype you are using.

in your while loop, assign this variable as MyDictionary.ElementAt(intCounter)
You can also directly access the Key and value as
Key → MyDictionary.ElementAt(intCounter).Key
Value → MyDictionary.ElementAt(intCounter).Value

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.