Click here to Skip to main content
15,885,065 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query on a table where nulls are allowed in the columns. The method then returns the query results as an array to plot a chart. The chart command is in a windows form and the database query is in a class.
I thought the query line
where P!=null
excluded nulls, but I still get an error
"can't convert double[]? to double"


There has to be a simple fix but I can't find anything on the net or MSDN .[^]

I'm going in circles...thanks for the help.

What I have tried:

C#
//a command in a windows form to plot a chart
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");
}


//code in a class to retrieve data from a table
namespace nmCHART
{
    public class clsCHART
    {
        public double?[] GET_Y_SERIES(string GREEK)        
        {

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

            return qryY;
        }
    }
}
Posted
Updated 17-Mar-16 18:16pm
v7
Comments
Garth J Lancaster 16-Mar-16 20:37pm    
how do you call GET_Y_SERIES("its-all-greek-to-me") ?

- for instance, if you have

double myGreekValue = GET_Y_SERIES("kali spera");

it will fail as Matt indicates, because myGreekValue is a single double, yet you return 'qryY.ToArray()' which is an array of values

so you'd need

double [] myGreekValues = GET_Y_SERIES("ti kanis");

for example as you have GET_Y_SERIES currently coded
Trader999 16-Mar-16 21:22pm    
Garth, I've added some more code - what you said made me think the problem maybe elsewhere. Basically I'm tying to plot a chart from a windows form when a command is pressed. Another class retrieves and returns the data as an array. Sorry I'm not sure how to format the code in the question.
Trader999 16-Mar-16 21:56pm    
...still getting the error msg!

You've declared the method to return a double but are trying to return double?[].

Edit [MTH]:
C#
//a command in a windows form to plot a chart
private void cmdCHART_Click(object sender, EventArgs e)
{
    // Does this really need to be created new each time it is used?
    // If nmCHART carries some important state, then it should be a singleton
    // Otherwise it should be a static class
    nmCHART.clsCHART oCHART = new nmCHART.clsCHART();
            
    double[] Y = oCHART.GET_Y_SERIES("CONDOR");
}
 

//code in a class to retrieve data from a table
namespace nmCHART
{
    public class clsCHART
    {
        public double[] GET_Y_SERIES(string GREEK)        
        {
            var qryY = (from P in Globals.DATA.PAYOFF_EAVs
                       where P.GREEK == GREEK
                             && P.DATA != null // as Sascha Lefèvre noted in Solution 2
                       orderby P.DP_NO
                       select P.DATA).OfType<double>().ToArray();
 
            return qryY;
        }
    }
}

The .OfType<double>() casts all of the values to double from double? so the caller can use it without having to be concerned about nulls.
Also, the .OfType<double>() will filter out the null values, so the null check in the query is not necessary, but, if this is actually a database query, the null check would be executed on the server side, so it could be more efficient.
 
Share this answer
 
v2
Comments
Trader999 16-Mar-16 19:54pm    
not sure if this is what you mean...but changing method type from double to double?[] still gives a compile error on the return line
Trader999 18-Mar-16 0:15am    
thanks Matt nice solution. I probably don't need to create the new class each time, once the code is working I'll improve the method. The graph will change repeatedly each time the user makes a change to their requirements.
You select P.DATA

So you should check P.DATA for not being null, not P:
C#
var qryY = (from P in Globals.DATA.PAYOFF_EAVs
            where P.GREEK == GREEK
            && P.DATA != null
            orderby P.DP_NO
            select P.DATA
           ).ToArray();
 
Share this answer
 
Comments
Trader999 18-Mar-16 0:13am    
thanks Sascha, I tried this but kept getting the same error
here is another solution with the help of others

C#
double[] Y = new double[21];
            Y = oCHART.GET_Y_SERIES("CONDOR").Where(d => d.HasValue).Cast<double>().ToArray();
 
Share this answer
 
v2
Comments
Matt T Heffron 18-Mar-16 14:38pm    
This moves the handling of the nulls out of the GET_Y_SERIES method and makes it the responsibility of all of the callers.
(You don't need to allocate the "new double[21]" since you are about to replace it on the next line.)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900