使用空值查询表 [英] Query on table with nulls
问题描述
我对一个表中有一个查询,其中列中允许空值。然后,该方法将查询结果作为数组返回以绘制图表。 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 adouble
but are trying to returndouble?[]
.
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 null
s.
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 selectP.DATA
So you should checkP.DATA
for not being null, notP
:
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屋!