Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在SQL中Linq计数为空_Sql_Linq_Count_Null - Fatal编程技术网

在SQL中Linq计数为空

在SQL中Linq计数为空,sql,linq,count,null,Sql,Linq,Count,Null,我正在尝试将以下sql转换为linq select sum(case when answer is null then 1 else 0 end) as numAnaswered from questions where id =@id 使用linq计数似乎很容易,但当id不在表中时,它给出0。而上面的sql返回null 最简单的LINQ是什么?您可以尝试以下方法: var numAnaswered= db.Questions.Where(q=>q.Id== id).Sum(q=

我正在尝试将以下sql转换为linq

select 
  sum(case when answer is null then 1 else 0 end) as numAnaswered 
from questions
where id =@id
使用linq计数似乎很容易,但当id不在表中时,它给出0。而上面的sql返回null

最简单的LINQ是什么?您可以尝试以下方法:

var numAnaswered= db.Questions.Where(q=>q.Id== id).Sum(q=>q.answer==null?(int?)1:0);
您可以尝试以下方法:

var numAnaswered= db.Questions.Where(q=>q.Id== id).Sum(q=>q.answer==null?(int?)1:0);

你在用EF吗?你在用EF吗?有什么区别?对于空的输入序列,结果仍然是0。Lol:)不管怎样,唯一可行的方法是使用
点击
int?
Sum
重载?(int?1:0)
例如。它将与L2E一起工作,L2O被破坏,并且将始终返回0(关于那个奇怪的行为有一个问题)。好的,我明白你的意思,让我来修复它,谢谢;),但是要返回
null
条件将不是
q.answer==null?(int?)1:null
?嗯,我不知道,再次感谢你,你是这方面的mvp;)是的,我在用同样的问题检查老问题,像这样:有什么区别?对于空的输入序列,结果仍然是0。Lol:)不管怎样,唯一可行的方法是使用
点击
int?
Sum
重载?(int?1:0)
例如。它将与L2E一起工作,L2O被破坏,并且将始终返回0(关于那个奇怪的行为有一个问题)。好的,我明白你的意思,让我来修复它,谢谢;),但是要返回
null
条件将不是
q.answer==null?(int?)1:null
?嗯,我不知道,再次感谢你,你是这方面的mvp;)是的,我在用同样的问题检查老问题,像这样: