开发者

Combine arrays by element into jagged array

开发者 https://www.devze.com 2023-01-17 21:49 出处:网络
I have a method which accept jagged array of Objects. public void MyDataBind(object[][] data) I use it like this

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消