使用空值查询表 [英] Query on table with nulls

查看:97
本文介绍了使用空值查询表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个表中有一个查询,其中列中允许空值。然后,该方法将查询结果作为数组返回以绘制图表。 chart命令采用Windows窗体,数据库查询位于类中。

我认为查询行

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"





必须有一个简单的修复,但我在网上找不到任何东西或MSDN 。[ ^ ]



我正在圈子里...感谢您的帮助。



我尝试了什么:





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:

//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;
        }
    }
}

推荐答案

您已声明方法返回 double 但是试图返回 double?[]



编辑[MTH]:

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

Edit [MTH]:
//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;
        }
    }
}



.OfType< double>() double?将所有值转换为 double ,以便调用者可以使用它而不必担心 null s。

此外, .OfType< double>()将过滤掉 null 值,因此查询查询时不需要 null ,但是,如果这实际上是数据库查询, null 检查将在服务器端执行,因此可能更有效。


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.


选择 P.DATA



所以你应该检查 P.DATA 因为不是空的,不是 P

You select P.DATA

So you should check P.DATA for not being null, not P:
var qryY = (from P in Globals.DATA.PAYOFF_EAVs
            where P.GREEK == GREEK
            && P.DATA != null
            orderby P.DP_NO
            select P.DATA
           ).ToArray();


这是另一个解决方案的帮助其他



here is another solution with the help of others

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


这篇关于使用空值查询表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆