2

I have a method which accept jagged array of Objects.

public void MyDataBind(object[][] data)

I use it like this

GoogleChart1.MyDataBind(new[] { new object[] { "September 1", 1 }, new object[] { "September 2", 10 } });

I have source data in two arrays like these and want to pass them to the method:

var sDate = new string[] {"September 1", "September 2"};
var iCount = new int[] { 1, 2 };

How can I pass, cast or transform these predefined array values to this method?

2 Answers 2

4

If you're using .NET 4 then the Zip method could be used to merge the two arrays:

MyDataBind(sDate.Zip(iCount, (s, i) => new object[] { s, i }).ToArray());
Sign up to request clarification or add additional context in comments.

Comments

3

EDIT:

even simpler and cleaner:

 var result = sDate.Select((s, index) => new object[] { s, iCount[index] }).ToArray();

A simple solution:

    List<object> items = new List<object>();
    for (int i = 0; i < sDate.Length; i++)
        items.Add(new object[] { sDate[i], iCount[i] });
    var result = items.ToArray();

You can define a method Combine(T[] array1, T[] array2) so get a more generic solution.

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.