Search code examples
c#linqhighchartsdotnethighcharts

Converting double?[ ] to object[ ] in c#


I am using DotNet.Highcharts in my C# program. The data element for series items requires an object[]. The data I’m using is coming from a LINQ method as shown below. The code below also converts the double?[] to a List<object> then to the desired object[].

double?[] data = (from c in context.CTSeries
                 select c.CTDI).Take(1000).ToArray();

List<object> dataList = new List<object>();

foreach (double? ctdi in data)
{
    dataList.Add( Convert.ChangeType(ctdi, typeof(Object)));
}

object[] dataArray = dataList.ToArray();
return dataArray;

Is there a better/more efficient way of getting the object[] from a double?[]?


Solution

  • Use LINQ and cast double? to object

    List<object> dataList = data.Select(d => (object)d).ToList();
    

    or to return an array

    return data.Select(d=> (object)d).ToArray();