[英]How to check if object is null in EF
I am new to EF and trying to do a small project with it. 我是EF的新手,正在尝试使用它做一个小项目。 I added a condition to EF but I am having a problem.
我向EF添加了条件,但遇到了问题。 My condition is all about IN condition like SQL,
SELECT * FROM table1 WHERE col1 IN (1,2,3...)
我的条件全都涉及IN条件,例如SQL,
SELECT * FROM table1 WHERE col1 IN (1,2,3...)
Here is my EF.... 这是我的EF。
var res3 = res2.Where(l => !slitDetail
.Any(s => s.BlockId == l.Id
&& s.WarehouseDepot.WarehouseDepotName != "Ara Ürün Depo"
&& s.WarehouseDepot.WarehouseDepotName != "Özel Kesim Depo"));
s.WarehouseDepot
might be NULL sometimes which is normal, but if it is null, this query throws an exception. s.WarehouseDepot
有时可能为NULL,这很正常,但如果为null,则此查询将引发异常。
How can I check if s.WarehouseDepot
is null and make it work even if it is null? 如何检查
s.WarehouseDepot
是否为空,即使它为空,也可以正常工作?
There are 2 possiblities if s.WarehouseDepot == null
如果
s.WarehouseDepot == null
则有2种s.WarehouseDepot == null
1) You want your Any
to return true
, in that case you could use something like 1)您希望您的
Any
返回true
,在这种情况下,您可以使用类似
var res3 = res2.Where(l => !slitDetail
.Any(s => s.BlockId == l.Id
&& s.WarehouseDepot != null
? (s.WarehouseDepot.WarehouseDepotName != "Ara Ürün Depo" && s.WarehouseDepot.WarehouseDepotName != "Özel Kesim Depo")
: true));
This would use the s.WarehouseDepot
only if it has a value otherwise it would return true 仅当它具有值时,才使用
s.WarehouseDepot
否则将返回true
2) You want your Any
to return false
. 2)您希望您的
Any
返回false
。 In this case you could simply replace the true
by false
in the above expression or use something like 在这种情况下,您可以在上述表达式中将
true
替换为false
或使用类似
var res3 = res2.Where(l => !slitDetail
.Any(s => s.BlockId == l.Id
&& s.WarehouseDepot != null
&& s.WarehouseDepot.WarehouseDepotName != "Ara Ürün Depo"
&& s.WarehouseDepot.WarehouseDepotName != "Özel Kesim Depo"));
Note that both these outcomes will automatically consider the s.BlockId == l.Id
condition too. 请注意,这两个结果也会自动考虑
s.BlockId == l.Id
条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.