我想将linq结果分配给double数组。我有两个对象数组。我按下面的方式取出它们:
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 }
})
...谢谢
答案 0 :(得分: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();
答案 1 :(得分: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]
。另外,我假设日期应该放在'first'中。