如何将空值存储到字典C#

我发现,如果我在数据库中的值为null,则在将其存储到字典时该应用程序将崩溃,这是我的代码:

private async Task<Dictionary<string, string>> GetProducts(long productId)
{
    return await _dbContext.Products
       .Where(prod => prod.productId == productId)
       .Select(prod => new
       {
           prod.ProductStockReferenceId,
           prod.ProductTitle
       })
       .ToDictionaryAsync(x => x.ProductStockReferenceId, x => x.ProductTitle);
}

And I've realized that if ProductStockReferenceId in database is null the app would crash, how come and what could I do to prevent this?

谢谢

干杯

评论