0

我想将 linq 结果分配给双数组。我有两个对象数组。我像下面这样获取它们:

var chartSeries = MeterReadings.GroupBy(x => x.Name)
                  .Select(g => new
                  {
                     Name = g.Key,
                     Data = g.Select(x => x.Value).ToArray(),
                     Date = g.Select(x => x.ReadDate).ToArray()
                  }).ToArray();

并尝试分配代码:

foreach (var item in chartSeries)
{
    int length = item.Data.Length;
    object[,] data =  ???  //first array item.Data and second item.Dates
    Series localSeries = new Series 
                         {
                              Name = item.Name, Data = new Data(data), Type = ChartType 
                         };
     Series.Add(localSeries);
 }

这一行object[,] data = ??? 我应该写什么?我想要类似的东西object[,] data = { item.Data, item.Dates }

我试图得到这样的:

Data = new Data(new object[,]
       {
          { new DateTime(1970, 9, 27), 0 },
          { new DateTime(1970, 10, 10), 0.6 },
          { new DateTime(1970, 10, 18), 0.7 },
          { new DateTime(1970, 11, 2), 0.8 },
          { new DateTime(1970, 11, 9), 0.6 },
          { new DateTime(1970, 11, 16), 0.6 },
          { new DateTime(1970, 11, 28), 0.67 },
          { new DateTime(1971, 1, 1), 0.81 },
          { new DateTime(1971, 1, 8), 0.78 },
          { new DateTime(1971, 1, 12), 0.98 },
          { new DateTime(1971, 1, 27), 1.84 },
          { new DateTime(1971, 2, 10), 1.80 },
          { new DateTime(1971, 2, 18), 1.80 },                                                            
          { new DateTime(1971, 6, 12), 0 }
       })

谢谢...

4

2 回答 2

4

我更喜欢使用 var :

var data = new object[2][];
data[0] = item.Data.Cast<Object>().ToArray();
data[1] = item.Date.Cast<Object>().ToArray();

根据您提供的精度,您可以使用:

var data = item.Date.Zip(item.Data, (n, m) => new[] { n as object, m as object }).ToArray();
于 2012-11-22T08:09:34.053 回答
1

像这样的东西应该做你需要的:

object[,] data = new object[2,length];

for (int i; i < length; i++)
{
    data[0, i] = item.Date[i];
    data[1, i] = item.Data[i];
}

您将需要验证应该如何构造数组 (object[2,length]object[length,2]。另外,我假设日期应该放在“第一个”中。

于 2012-11-22T08:04:49.110 回答