1

Having an DataView tmp, I need to create from two columns of tmp an array of array (probably this is not the correct term in c#, I'm new to it) to be passed to DotNet.Highcharts. The final structure should be something like:

 [[5, 2], [6, 3], [8, 2]]

First, I used this code:

Object[] result = new object[tmp.Count];
result = tmp.Table
            .AsEnumerable()
            .Select(row => new Object[] { row.Field<DateTime>("DATE"),
                                            Convert.ToDouble(row.Field<string>("VALUE"))})
            .ToArray();

The problem came when I try to substitute an element of result with a Point (to define custom marker e.g.):

if (something[k] == "TRUE") // if for a given row a generic condition is true
{
    result[k] = new DotNet.Highcharts.Options.Point
    {
        X = Tools.GetTotalMilliseconds((DateTime)tmp[k].Row["DATE"]),
        Y = Convert.ToInt32(tmp[k].Row["VALUE"]),
        Marker = new PlotOptionsSeriesMarker
        {
            Symbol = "url(/Proj/images/cross.png)"
        }
    };
}

In runtime I get: A first chance exception of type 'System.ArrayTypeMismatchException' occurred in App_Web_pz3kxbru.dll

I found a fix doing this to create result:

Object[] result = new object[tmp.Count];
for (int j = 0; j < tmp.Count; j++)
{
      result [j] = new object[] { (DateTime)tmp[j].Row["DATE"],
                                    Convert.ToDouble(tmp[j].Row["VALUE"])};
}

Now everything works fine. However I don't have any clue of what actually I did... Can you please explain me the difference between the methods I used to obtain result.

1 Answer 1

1

The only difference I can notice is, Convert.ToDouble in the first approach :

.Select(row => new Object[] { row.Field<DateTime>("DATE"),
                                            Convert.ToDouble(row.Field<string>("VALUE"))})

versus Convert.ToInt32 in the second :

new object[] { (DateTime)tmp[j].Row["DATE"],
                                    Convert.ToInt32(tmp[j].Row["VALUE"])};

UPDATE :

The first approach create a new array, replacing the original array which is array of object (Object[]) with new array which is 'array of array' (Object[][]), hence causes a problem (co-variant array problem).

The second approach doesn't create a new array, but fill in each slot in the original array of object with an array. So in this case, result is still array of object, doesn't get changed to 'array of array', hence can avoid exception.

If you favor the LINQ approach, this will do (still need a counter variable) :

Object[] result = new object[tmp.Count];
int i = 0;
result = tmp.Table
            .AsEnumerable()
            .ForEach(row => result[i++] = new Object[] { row.Field<DateTime>("DATE"),
                                            Convert.ToDouble(row.Field<string>("VALUE"))});

or better yet, simply cast the array of object to object :

result = tmp.Table
            .AsEnumerable()
            .Select(row => (Object)new Object[] { row.Field<DateTime>("DATE"),
                                            Convert.ToDouble(row.Field<string>("VALUE"))})
            .ToArray();
Sign up to request clarification or add additional context in comments.

4 Comments

sorry was it was a "typo". The exception arises when code gets to the point of modifying result. So you think that both methods produce the same output?
after reading the question again, nope both approach act differently. The first returning a new array because of using .ToArray(), and unfortunately the new array is not array of object but can be assign to variable of type array of object (both type are co-variant).
Thanks a lot. I slowly start to understand, maybe... Per my current understanding, I see both the result as array with objects[] inside each item. I understand that the first method replaces result with a new array (in fact object[tmp.Count] is not needed in the definition, and without it I have the same result). What I'm still a bit confused about is why it's replaced with something different. You mentioned array of array, so it's that the output from the linq notation I used?
Oh didn't see the edit. Thanks again. Also for the co-variant array problem link.

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.