我有一个以下LINQ查询:
var productTypes = from ProductDto e in Product
                            select new 
                            {
                                Id = e.Product.ID, 
                                Name = e.Product.name 
                            };
在上面的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"
                            };
| 归档时间: | 
 | 
| 查看次数: | 21393 次 | 
| 最近记录: |