简单的LINQ查询: from transport in db.Transports select new { Current = transport.CurrentLocation, CurrentCarriers = transport.CurrentLocation.Carriers, }; 问题:CurrentLocation可能为null.如果是,则执行此查询会抛出Nul
from transport in db.Transports select new { Current = transport.CurrentLocation, CurrentCarriers = transport.CurrentLocation.Carriers, };
问题:CurrentLocation可能为null.如果是,则执行此查询会抛出NullReference.我尝试添加支票
transport.CurrentLocation == null ? null : transport.CurrentLocation.Carriers
但Linq to sql似乎无法解析那个.
任何不错的解决方案,不涉及为每个传输发送额外的查询?
我通常只是使用’let’.from x in Foo let y = x.Bar where y != null select y.Baz;
更新:
我觉得 ??运算符确实转换为SQL.