C#类型转换

本文关键字:类型转换 | 更新日期: 2023-09-27 18:01:01

public static Series MeterReadingsToPieChart(IEnumerable<Dictionary<string, double>> readings)
{
    object[] points = ? // I dont know how can I create this array from readings.
    Series series = new Series
    {
        Type = ChartTypes.Pie,
        Name = "",
        Data = new Data(points)
    };
    return series;
}

我想从上面的代码中动态创建以下内容

Series series = new Series
{
    Type = ChartTypes.Pie,
    Name = "",
    Data = new Data(new object[]
           {
               new object[] { "Gaziemir", 45.0 },
               new object[] { "Bornova", 26.8 },
               new object[] { "Forum Bornova", 8.5 }
           })
};

你能指出起点吗?

感谢

C#类型转换

要匹配底部的代码,应该是:

object[][] points = readings.Select(
      pair => new object[] { pair.Key, pair.Value }).ToArray();
Series series = new Series
{
    Type = ChartTypes.Pie,
    Name = "",
    Data = new Data(points))
};

然而,就我个人而言,object[][]并不是最好的选择。。。