0

I have this string

string[] xInBGraph = { "IVR", "Agents", "Abandoned", "Cancelled" };

and I have these values:

int ivr = 1;
int agents = 2;
int abandoned = 3;
int cancelled = 4;

What I need

To make an array for each element in the array xInBGraph in which the new array should contain one value and the other values are zero. For example This is how the final result will be

IVR = [ivr =1, 0 , 0 ,0, 0]
Agents = [0, agents=2, 0,0]
Abandoned = [0, 0, abandoned = 3, 0]
Cancelled = [0, 0, 0, cancelled = 0]

what I have tried

making 4 arrays and fill them in the correct data. It works good. However, my altimate goal is to transfer that final result to a json object. I need to return just on json object. but in my case, which is 4 arrays, I must return 4 json objects which is not good for my situation. I need to return just on json object. So, what is the object in c# that can have the mentioned data and can be transfer to one json object?

I am using json.net library so I can easily change any c# object to json object

Edit

I made these four arrays:

int[] ivrArray = { Tivr, 0, 0, 0};
int[] agentsArray = { 0, tTotalCallsByAgent, 0, 0 };
int[] abandonedArray = { 0, 0, tTotalAbandoned, 0};
int[] canceledArray = { 0, 0, 0, Tcancel};

Now all I need is something to save the label of each array and the array in one row.

2 Answers 2

3

I would suggest you use a Dictionary. Specifically,

Dictionary<string,int[]> dictionary = new Dictionary<string,int[]>()
{
    { "IVR", new int[] {1,0,0,0} },
    { "Agents", new int[] {0,2,0,0} },
    { "Abandoned", new int[] {0,0,3,0} },
    { "Cancelled", new int[] {0,0,0,0} },    
}
Sign up to request clarification or add additional context in comments.

3 Comments

I copied your code, but I got a lot of red errors. for example, no overload for method take 1 argument and invalid anoymous type member declared ... why please?
please give me a few minutes to fix it. Thanks for being patient.
could you follow me here please stackoverflow.com/questions/23028526/…
0

Hope this is what you are expecting

    string[] xInBGraph = { "IVR", "Agents", "Abandoned", "Cancelled" };

    List<string[]> final = new List<string[]>();
    for (int i = 0; i < xInBGraph.Count(); i++)
    {
        List<string> array = new List<string>();
        for (int x = 0; x < xInBGraph.Count(); x++)
        {
            if (x == i)
            {
                array.Add(xInBGraph[i].ToString() + "=" + x);
            }
            else
            {
                array.Add("0");
            }
        }
        final.Add(array.ToArray());
    }
    string json = JsonConvert.SerializeObject(final, Formatting.Indented);

Output
[ [ "IVR=0", "0", "0", "0" ], [ "0", "Agents=1", "0", "0" ], [ "0", "0", "Abandoned=2", "0" ], [ "0", "0", "0", "Cancelled=3" ] ]

Comments

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.