如何让Null Coalesce运算符在ASP.NET MVC Razor中工作?

时间:2022-06-16 02:31:32

I have the following, but it's failing with a NullReferenceException:

我有以下内容,但它失败了NullReferenceException:

<td>@item.FundPerformance.Where(xx => fund.Id == xx.Id).FirstOrDefault().OneMonth ?? -</td>

OneMonth is defined as

OneMonth定义为

public virtual decimal? OneMonth { get; set; }

and its value is null at the time that it fails.

并且在失败时它的值为null。

I thought the Null Coalesce operator would test if its null and if so, return the value to the right of the operator?

我认为Null Coalesce运算符会测试它是否为null,如果是,则将值返回给运算符右侧?

What do I need to change to make this work?

我需要做些什么来改变这项工作?

2 个解决方案

#1


23  

The razor syntax, as you wrote it, ends at "OneMonth". The ?? are interpreted as text. To have it interpreted as razor, you must wrap the whole statements in () like this:

您编写的剃刀语法以“OneMonth”结尾。 ?? ??被解释为文本。要将它解释为剃刀,您必须将整个语句包装在()中,如下所示:

<td>@(item.FundPerformance.Where(xx => fund.Id == xx.Id).FirstOrDefault().OneMonth ?? "-")</td>

This will still get you an error: the left operator is a decimal and the right operator is a string. So you can either render a zero instead of "-" or use ternary operator, with OneMonth.Value.ToString() as left value and "-" as right value.

这仍然会给你一个错误:左操作符是小数,右操作符是字符串。因此,您可以渲染零而不是“ - ”或使用三元运算符,OneMonth.Value.ToString()作为左值,“ - ”作为右值。

#2


2  

It's nothing to do with MVC or Razor.

这与MVC或Razor无关。

FundPerformance.Where(xx => fund.Id == xx.Id).FirstOrDefault()

will return null if there is no element that matches, null does not have a OneMonth porperty so you will get a null ref exception. You cannot use the ?? operator as it is not OneMonth that is null, it is the result of FirstOrDefault().

如果没有匹配的元素,则返回null,null没有OneMonth属性,因此您将获得null ref异常。你不能用??运算符,因为它不是OneMonth,它是null,它是FirstOrDefault()的结果。

To test change your code to

要测试将代码更改为

FundPerformance.Where(xx => fund.Id == xx.Id).First().OneMonth ?? -</td>

If you get a "sequence contains no elements" exception instead then you know that is your problem.

如果你得到一个“序列不包含元素”的异常,那么你知道这是你的问题。

#1


23  

The razor syntax, as you wrote it, ends at "OneMonth". The ?? are interpreted as text. To have it interpreted as razor, you must wrap the whole statements in () like this:

您编写的剃刀语法以“OneMonth”结尾。 ?? ??被解释为文本。要将它解释为剃刀,您必须将整个语句包装在()中,如下所示:

<td>@(item.FundPerformance.Where(xx => fund.Id == xx.Id).FirstOrDefault().OneMonth ?? "-")</td>

This will still get you an error: the left operator is a decimal and the right operator is a string. So you can either render a zero instead of "-" or use ternary operator, with OneMonth.Value.ToString() as left value and "-" as right value.

这仍然会给你一个错误:左操作符是小数,右操作符是字符串。因此,您可以渲染零而不是“ - ”或使用三元运算符,OneMonth.Value.ToString()作为左值,“ - ”作为右值。

#2


2  

It's nothing to do with MVC or Razor.

这与MVC或Razor无关。

FundPerformance.Where(xx => fund.Id == xx.Id).FirstOrDefault()

will return null if there is no element that matches, null does not have a OneMonth porperty so you will get a null ref exception. You cannot use the ?? operator as it is not OneMonth that is null, it is the result of FirstOrDefault().

如果没有匹配的元素,则返回null,null没有OneMonth属性,因此您将获得null ref异常。你不能用??运算符,因为它不是OneMonth,它是null,它是FirstOrDefault()的结果。

To test change your code to

要测试将代码更改为

FundPerformance.Where(xx => fund.Id == xx.Id).First().OneMonth ?? -</td>

If you get a "sequence contains no elements" exception instead then you know that is your problem.

如果你得到一个“序列不包含元素”的异常,那么你知道这是你的问题。