0

在下面的代码中transmittalno.TransID总是有值,但doctranstocon.Transid有时有值,有时没有,所以当doctranstocon.Transid有值时我没有问题,但当没有任何值时,在 where 子句中比较时,如 : transmittalno.TransID == doctranstocon.Transid。它返回错误“对象引用未设置为对象的实例。

TranstoCons = from doctranstocon in _DocTranstoCons where 
              ( transmittalno.TransID == doctranstocon.Transid  ) 
              select doctranstocon.tblTranstoCon

当“ doctranstocon.Transid ”没有任何值时,我喜欢 TranstoCons 返回 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 
             };
4

2 回答 2

0

你可以使用这个:

where doctranstocon != null && doctranstocon.Transid.Equals(transmittalno.TransID)
于 2012-10-18T05:09:26.223 回答
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
             };
于 2012-10-18T05:09:38.050 回答