验证if子句中的空查询

我有一个简单的Linq查询,并且if...else条件类似:


var incidenciaPronosticada = _c.IncidenciaPronosticadas

    .Where( x => x.IdIncidenciaPronosticada == item.IdIncidenciaPronosticada )

    .FirstOrDefault();


if( incidenciaPronosticada.IdIncidenciaPronosticada != 0 || incidenciaPronosticada.IdIncidenciaPronosticada != null )

{

    //code there

}

else {

    //code there

}

问题是当incidenciaPronosticada它为null时不执行else子句,它只是停止


incidenciaPronosticada.IdIncidenciaPronosticada ='incidenciaPronosticada.IdIncidenciaPronosticada'引发了类型'System.NullReferenceException'的异常


为什么不读incidenciaPronosticada.IdIncidenciaPronosticada != null?如果调试器读取了它,它应该传递给else子句否?还是我做错了什么?问候


跃然一笑
浏览 144回答 2
2回答

慕田峪9158850

incidenciaPronosticada本身可以为null,这incidenciaPronosticada.IdIncidenciaPronosticada != 0将导致aNullReferenceExecption因为IdIncidenciaPronosticada无法访问。执行显式的null检查或使用null安全导航操作符?.:if( incidenciaPronosticada != null && incidenciaPronosticada.IdIncidenciaPronosticada != 0 ) {或者:if( incidenciaPronosticada?.IdIncidenciaPronosticada > 0 ) {(请注意我用的> 0,而不是!= 0因为> 0会true如果incidenciaPronosticada是空的(因为(Int32?)null != (Int32?)0):还要注意,incidenciaPronosticada?.IdIncidenciaPronosticada表达式的类型是Nullable<Int32>(aka Int32?)而不是Int32。这是下表的结果 if( incidenciaPronosticada.IdIncidenciaPronosticada ... )incidenciaPronosticada&nbsp; &nbsp; &nbsp; IdIncidenciaPronosticada&nbsp; &nbsp; . != 0&nbsp; &nbsp; . > 0null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws&nbsp; &nbsp; throws(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; true&nbsp; &nbsp; &nbsp; false(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;false&nbsp; &nbsp; &nbsp;false(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;true&nbsp; &nbsp; &nbsp; true这是下表的结果 if( incidenciaPronosticada?.IdIncidenciaPronosticada ... )incidenciaPronosticada&nbsp; &nbsp; &nbsp; IdIncidenciaPronosticada&nbsp; &nbsp; ?. != 0&nbsp; &nbsp;?. > 0null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; true&nbsp; &nbsp; &nbsp; false(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; true&nbsp; &nbsp; &nbsp; false(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;false&nbsp; &nbsp; &nbsp;false(object)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;true&nbsp; &nbsp; &nbsp; true

FFIVE

您正在尝试访问incidenciaPronosticada内部的成员。空支票应该看起来像if(incidenciaPronosticada != null){//code there}else {//code there}
打开App,查看更多内容
随时随地看视频慕课网APP