Search code examples
c#linqcastingnullable

cannot implicitly convert from double?[] to double[]


I'm trying to plot a chart by querying a table for the data, and the table columns are nullable. The query runs ok, but passing the query array back to the chart array raises an error "cannot implicitly convert from double?[] to double[]". I've searched the net without success. Any suggestions please?

//code in the windows form
 private void cmdCHART_Click(object sender, EventArgs e)
        {
            nmCHART.clsCHART oCHART = new nmCHART.clsCHART();

            double[] Y = new double[21];
            Y = oCHART.GET_Y_SERIES("CONDOR"); //can't cast double?[] to double[]
        }

//get data
public double?[] GET_Y_SERIES(string GREEK)
        {

            var qryY = (from P in Globals.DATA.PAYOFF_EAVs
                        where P.GREEK == GREEK
                        orderby P.DP_NO
                        select P.DATA).ToArray();

            return qryY;
        }

Solution

  • Use Cast to convert it to double.

    Y =   oCHART.GET_Y_SERIES("CONDOR").Cast<double>().ToArray();
    

    To avoid the null values in the conversion use this.

    Y =   oCHART.GET_Y_SERIES("CONDOR").Where(d=>d.HasValue).Cast<double>().ToArray();
    

    Working example