我有一个以下LINQ查询:
var productTypes = from ProductDto e in Product
select new
{
Id = e.Product.ID,
Name = e.Product.name
};
Run Code Online (Sandbox Code Playgroud)
在上面的LINQ查询中,e.Product可能是null
.但我没有找到方法.
谁能帮我吗 ?null
如果e.Product是,我想在productTypes变量中赋值null
.
Ehs*_*jad 16
您可以使用三元运算符检查null,如下所示:
var productTypes = from ProductDto e in Product
select new
{
Id = e.Product != null ? e.Product.ID : 0,
Name = "xyz"
};
Run Code Online (Sandbox Code Playgroud)