1

I have this code:

int[] ivrArray = { 1, 0, 0, 0};
int[] agentsArray = { 0, 2, 0, 0 };
int[] abandonedArray = { 0, 0, 3, 0};
int[] canceledArray = { 0, 0, 0, 4};
Dictionary<string, int[]> dictionary = new Dictionary<string, int[]>()
            {
                { "IVR", ivrArray },
                { "Agents", agentsArray },
                { "Abandoned", abandonedArray },
                { "Cancelled", canceledArray },    
            };

The output is

{
  "IVR": [
    1,
    0,
    0,
    0
  ],
  "Agents": [
    0,
    2,
    0,
    0
  ],
  "Abandoned": [
    0,
    0,
    3,
    0
  ],
  "Cancelled": [
    0,
    0,
    0,
    4
  ]
}

Is there anyway so the output will be like this:

"Cancelled":[
   [0],
   [0],
   [0],
   [4]
]

So each element is array of one element

3
  • 1
    @AvnerShahar-Kashtan that is a library that changes that c# object to json. it does't matter what the object is. It can change any object to json object. it is json.net library. but my question is how to make that c# object contains array of arrays , not just arrays. got me ? Commented Apr 12, 2014 at 9:23
  • Are you asking how to rewrite the code so that it creates a set of int[][] instead of int[], or asking how to write code which converts the given int[] to int[][]? Commented Apr 12, 2014 at 9:23
  • @AvnerShahar-Kashtan the first one, hot to change int[] to int[][],i tried myself to do like this: int[][] ivrArray = { [1], [0], [0], [0]}; but I got syntax error Commented Apr 12, 2014 at 9:25

2 Answers 2

2

You can re-project your arrays to a Dictionary<string, int[][]> like so:

 var dictionary = new Dictionary<string, int[][]>
    {
       {"IVR", ivrArray.Select(_ => new[] {_}).ToArray()},
       {"Agents", agentsArray.Select(_ => new[] {_}).ToArray()},
       {"Abandoned", abandonedArray.Select(_ => new[] {_}).ToArray()},
       { "Cancelled",canceledArray.Select(_ => new[] {_}).ToArray()}
    };

(and I guess if you didn't want the local vars, then

 ...
   {"IVR", new [] { 1, 0, 0, 0 }.Select(_ => new[] {_}).ToArray()},
 ...
Sign up to request clarification or add additional context in comments.

3 Comments

can i say Dictionary<string, int[][]> dictionary instead or var dictionary ?
@MarcoDinatsoli Y, sure, although the var variable is still strongly and correctly typed to Dictionary<string, int[][]> :)
could you help me here please stackoverflow.com/questions/23029743/…
1

you can do like this using Jagged Arrays concept:

for example:

int[] list1 = new int[4] { 1, 2, 3, 4};
int[] list2 = new int[4] { 5, 6, 7, 8};
int[] list3 = new int[4] { 1, 3, 2, 1 };
int[] list4 = new int[4] { 5, 4, 3, 2 };

int[][] lists = new int[][] {  list1 ,  list2 ,  list3 ,  list4  };

and in dictionary:

 Dictionary<string, int[][]> items= new Dictionary<string, int[][]>;

here is MSDN documentation:

http://msdn.microsoft.com/en-us/library/2s05feca.aspx

3 Comments

in this way, i lost the label in the dictinary
you need to make dictionary like: <string,int[][]>
As strautLC also posted

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.