在下面的代码transmittalno.TransID
中总是有值,但doctranstocon.Transid
有时有价值,有时却没有,所以当doctranstocon.Transid
有价值时我没有问题,但是当没有任何价值时,在where子句中比较时:transmittalno.TransID == doctranstocon.Transid
。
它返回错误“对象引用未设置为对象的实例。”
TranstoCons = from doctranstocon in _DocTranstoCons where
( transmittalno.TransID == doctranstocon.Transid )
select doctranstocon.tblTranstoCon
我喜欢TranstoCons在“doctranstocon.Transid”没有任何值时返回null
如何在以下linq查询中处理此错误:
var query = from transmittalno in _Transmittals
Select new TransmittaltoConPresentationModel
{
TransID = transmittalno.TransID,
Transmittal = transmittalno.TRANSMITTAL,
TranstoCons = from doctranstocon in _DocTranstoCons where
( transmittalno.TransID == doctranstocon.Transid )
select doctranstocon.tblTranstoCon
};
答案 0 :(得分:0)
您可以使用:
where doctranstocon != null && doctranstocon.Transid.Equals(transmittalno.TransID)
答案 1 :(得分:0)
var query = from transmittalno in _Transmittals
Select new TransmittaltoConPresentationModel
{
TransID = transmittalno.TransID,
Transmittal = transmittalno.TRANSMITTAL,
TranstoCons = from doctranstocon in _DocTranstoCons
where doctranstocon == null || transmittalno.TransID == doctranstocon.Transid
select doctranstocon != null? doctranstocon.tblTranstoCon : null
};