views:

78

answers:

2

I have such 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 } });

The question would be how to pass/cast predefined array values to this method? Let's say I have two arrays below and want to pass them to the method.

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

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.

testalino
+2  A: 

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());
LukeH