Search code examples
c#arrayscastingjagged-arrays

Combine arrays by element into jagged array


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?


Solution

  • 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.