我正在尝试使用virtual
从另一个表中的某个类型的实例获取价格总和,但要注意的是该实例可以为null,并且无论何时为null都会引发异常。有什么方法可以让类型为null并仍然获得属性的回报?
// Trying to get product price from products table, throws exception when Product is null
{order.Product.PriceTotal}
每当product为null时,都会引发异常。
答案 0 :(得分:0)
您要做的就是在类型之后添加空传播运算符?
。
// Trying to get product price from products table, throws exception when Product is null
{order.Product?.PriceTotal}
这意味着如果Product
为空,则会将PriceTotal
设置为空