简体   繁体   English

Linq和匿名类型

[英]Linq and anonymous type

select new
{
 Selected = (cvf != null && cvf.Deleted==false)
}

The above statement proceeds to check cvf.Deleted even if cvf is null. 上面的语句继续检查cvf。即使cvf为null也被删除。 Then it throws an invalid object reference error. 然后,它将引发无效的对象引用错误。

How do I fix this? 我该如何解决?

Probably something else going on since && will short-circuit evaluate . 由于&& 可能会 短路评估,因此可能正在发生其他情况。 That said, try this instead: 也就是说,请尝试以下操作:

select new
{
    Selected = cvf != null
        ? !cvf.Deleted
        : false
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM