gpt4 book ai didi

c# - Linq 选择新列表属性空值检查

转载 作者:太空狗 更新时间:2023-10-29 19:55:51 25 4
gpt4 key购买 nike

我有一个下面的 LINQ 查询:

var productTypes = from ProductDto e in Product
select new
{
Id = e.Product.ID,
Name = e.Product.name
};

在上面的 LINQ 查询中,e.Product 可能是 null。但我没有办法找出答案。

谁能帮帮我?如果 e.Product 为 null,我想在 productTypes 变量中分配 null

最佳答案

您可以使用 ternary operator 检查 null像这样:

var productTypes = from ProductDto e in Product
select new
{
Id = e.Product != null ? e.Product.ID : 0,
Name = "xyz"
};

关于c# - Linq 选择新列表属性空值检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24675454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com