小编Nic*_*ick的帖子

Linq查询在空时不返回null

我正在使用.NetCore 1.1.2.当我为GET动作使用生成的动作时,它应该在DB上没有找到任何内容时返回null,但是Null验证不起作用.

我尝试使用Single()而不是SingleOrDefault使用try catch来抛出异常,但它似乎不是一个优雅的解决方案.

也许这是.net版本?

public IActionResult Success(string token)
{
    var paymentMade = _context.Payments
            .SingleOrDefaultAsync(m => m.StripeToken == token);

    if (paymentMade == null)
    {
        return NotFound(); //this never gets fired when empty
    }

    return View();
}
Run Code Online (Sandbox Code Playgroud)

.net c# linq asp.net-core

2
推荐指数
1
解决办法
773
查看次数

标签 统计

.net ×1

asp.net-core ×1

c# ×1

linq ×1