-1

I have a dictionary from which I like to create a multidimensional array through c#.

foreach (KeyValuePair<string, int> pair in rptdata)
{
    string s2 = pair.Key;        
    int s1 =  pair.Value;

    // var ccdata1 = new[] { new object[] { "Item1", 1 }  };
    // object value = cdata1[s1,s1];                                         
}

I need to add code inside the foreach look so that it can create something like the following:

var ccdata = new[] { new object[] { "Item1", 1 }, new object[] { "Item2", 2 } };

Note that Item1,Item2 would come from str1 and 1,2 would come from int1.

I am not sure how to iterate though to populate the multidimensional object array.

1 Answer 1

1

You could do it in one query like this. Linq is pretty great at transforming data. If you want to turn it into JSON afterwards you can use a library, something like Json.NET.

var ccdata = rptdata
               .Select( i => new object[]{ i.Key, i.Value } )
               .ToArray();
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your response, Where would this code go? Would it go inside of the loop? Can you put it with the existing code so that I can see what it looks like? Also, will the code you mentioned above create a multidimensional array? Also how do I assign the array to ccdata? Thanks in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.