加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – Linq to sql:属性的属性,可以为null

发布时间:2020-12-16 01:30:02 所属栏目:百科 来源:网络整理
导读:简单的LINQ查询: from transport in db.Transports select new { Current = transport.CurrentLocation,CurrentCarriers = transport.CurrentLocation.Carriers,}; 问题:CurrentLocation可能为null.如果是,则执行此查询会抛出NullReference.我尝试添加支票
简单的LINQ查询:

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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读