简体   繁体   English

空合并运算符不接受不同类型

[英]The null-coalescing operator doesn't accepts different types

I started out, returning a custom class:我开始,返回一个自定义类:

[HttpGet()]
public ActionResult<Round> Get(string id) =>
    this._roundsService.Get(id);

The Get method in the rounds service can return null and that gets converted to HTTP 204 No Content. rounds 服务中的 Get 方法可以返回 null 并转换为 HTTP 204 No Content。 I was wondering how to return 404 when I get null:我想知道当我得到空值时如何返回 404:

[HttpGet()]
public ActionResult<Round> Get(string id) =>
    this._roundsService.Get(id) ?? NotFound();

Apperantly this doesn't work and gives me a CS0019 error: Operator '??' cannot be applied to operands of type 'Round' and 'NotFoundResult'显然这不起作用并给我一个 CS0019 错误: Operator '??' cannot be applied to operands of type 'Round' and 'NotFoundResult' Operator '??' cannot be applied to operands of type 'Round' and 'NotFoundResult'

I'm open to other one-liners that would return the desired object if not null and 404 if null.我对其他单行程序持开放态度,如果不为空则返回所需的对象,如果为空则返回 404。

I'm using C# 8.0 with the netcoreapp3.0 framework.我将 C# 8.0 与 netcoreapp3.0 框架一起使用。 I haven't enabled the nullable feature.我还没有启用可为空功能。 Could that be causing the problem?这可能是导致问题的原因吗?

Just in case, here's the method in the service class:以防万一,这里是服务类中的方法:

public Round Get(string id) =>
    this._rounds.Find(round => round.Id == id).FirstOrDefault();

When you call NotFound() , you are creating a NotFoundResult .当您调用NotFound() ,您正在创建一个NotFoundResult Your method has a return type of ActionResult<Round> but NotFoundResult doesn't actually inherit from ActionResult<Round> , so you cannot return the NotFoundResult object directly.您的方法具有ActionResult<Round>的返回类型,但NotFoundResult实际上并未从ActionResult<Round>继承,因此您不能直接返回NotFoundResult对象。

When you do type return NotFound() then what actually happens is that the compiler will use the implicit operator ActionResult<T> (ActionResult) to transform the NotFoundResult into a ActionResult<Round> .当您键入return NotFound() ,实际发生的是编译器将使用隐式运算符ActionResult<T> (ActionResult)NotFoundResult转换为ActionResult<Round>

This works fine when you just return the value directly but it will not work when used within a ternary conditional or null-coalescing expression.这在您直接返回值时工作正常,但在三元条件或空合并表达式中使用时将不起作用。 Instead, you would have to do the conversion yourself:相反,您必须自己进行转换:

public ActionResult<Round> Get(string id) =>
    this._roundsService.Get(id) ?? new ActionResult<Round>(NotFound());

Because the constructor of ActionResult<T> accepts any ActionResult , you can just pass the NotFoundResult to it to make sure that it gets converted properly.因为ActionResult<T>构造函数接受任何ActionResult ,您只需将NotFoundResult传递给它以确保它被正确转换。

Of course, you can also split this up again and have the compiler do the conversion for you:当然,您也可以将其再次拆分并让编译器为您进行转换:

public ActionResult<Round> Get(string id)
{
    var result = this._roundsService.Get(id);
    if (result != null)
        return result;
    return NotFound();
}

Of course it cannot do that.当然它不能那样做。 It is only shorthand for a if .它只是if简写。 I think the equivalent to what you wrote would be around this:我认为相当于你写的将围绕这个:

ActionResult<Round> result = this._roundsService.Get(id);
if(result == null)
  result = NotFound();
return result;

And at this point, the compiler is really confused why you try to assign the "NotFound()" return value to a ActionResult variable.在这一点上,编译器真的很困惑为什么您尝试将“NotFound()”返回值分配给 ActionResult 变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM