Consuming C# dictionary in AX2012

Hi,

        KARYA.AX2009DBADAPTOR test = new KARYA.AX2009DBADAPTOR();
        CLRObject listEnumerator,listEnumerator1;
        System.Collections.IEnumerable iList;
        CLRObject l1,clrObject,clrObject1;
        MapIterator mapIterator;
        //System.Collections.ArrayList list;
        int cnt,i;
        System.Collections.IEnumerator dotNetEnumerator;
        System.Collections.DictionaryEntry dotNetDictEntry;
        AnyType x;
        str tempValue;    
        container   con;

        CLRObject list = new CLRObject("System.Collections.Generic.List`1[System.String]");
        list = test.findRecord("ECL_CustHistory","RecId","RecId","5637203576");
        listEnumerator = list.getEnumerator();
        while(listEnumerator.moveNext())
        {
           
            l1 = new CLRObject("System.Collections.Generic.Dictionary`2[System.String,System.Object]");
            l1 = listEnumerator.get_Current();
            listEnumerator1 = l1.getEnumerator();
            while (listEnumerator1.MoveNext())
            {             
                //var element = listEnumerator1.get_Current();
                clrObject1 = new CLRObject("System.Collections.Generic.Dictionary`2[System.String,System.Object]");
                clrObject1 = listEnumerator1.get_Current();
            }

            ]![2364.dll.PNG|1122x458

(upload://zlwRblzn3BLHnfExkDT7r8ms5TU.png)

I have a dll and referenced in AX2012 to retrieve multiple records from external SQL database.The result from dll is an List consisting of dictionary with keyvalue pairs representing table fields and their values.

I have iterated the list and retrieved dictionary for each iteration but the problem is I can’t able to retrieve key value pairs from the dictionary.
Can anyone please let me know to iterate the dictionary from C# using X++ standard.

Thanks in Advance

Can you please elaborate what you mean by “I can’t able to retrieve key value pairs”?

By the way, there is a better solution (in my opinion) that dealing with weakly-typed CLRObject instances (at the cost of giving up generic collections).

Declare l1 as the (non-generic) interface System.Collections.IDictionary (assuming that it really is a dictionary); then you’ll be able to use its methods with code completion, compile-time control etc. I would do something similar with the list as well

Note that in this code:

l1 = new CLRObject("System.Collections.Generic.Dictionary`2[System.String,System.Object]");
l1 = listEnumerator.get_Current();

the first line has no effect, because the value is immediately overwritten on the second line.

Thanks for your valuable reply Martin.
Actually I modified the code and Assigned the Iterated value to IDictionary object,But there was a Cast conversion Error Raising and Can’t able to iterate that Idictionary object.
System.InvalidCastException: Unable to cast object of type ‘System.Collections.Generic.KeyValuePair`2[System.String,System.Object]’ to type ‘System.Collections.IDictionary’.

System.Collections.IDictionary dict;

dict = listEnumerator.get_Current();(Cast Conversion Error)

It says that you use a wrong type. Get get_Current() returns KeyValuePair<String, Object>, but the type of your variable is IDictionary.
If listEnumerator enumerator has still the same meaning as in your code above, it seems that findRecord() returns something else than you think. It seems to be a dictionary instead of a list of dictionaries.

yeah it Actually returns Dictionary only,but the problem is I can’t able to iterate that dictionary in X++ using system.collections.generic.dictionary<string,object>.There doesn’t seems to be any methods to retrieve key and value from the Iterated Dictionary in X++.

while (listEnumerator1.MoveNext())
{

clrObject1 = listEnumerator1.get_Current();(It Contains the Dictionary )
}

You won’t get any IntelliSense with CLRObject, that’s why I suggested using IDictionary. Consider following my suggestion, now when you’re at at the right place.

is there any way to Iterate the Idictionary using X++?

Of course - use an enumerator. You already did it at the point where you we’re getting KeyValuePair<String, Object> (your bug there was that you try to assign it to a variable of a wrong type).

Thank you Martin.