[英]Value cannot be null, r nparameter name source
我很困惑。 我有两个相同的语句,一个有效,另一个收到错误- 值不能为null。 参数名称来源。 从我的阅读中我收到错误,因为我的linq表达式中某些内容为空。 但是,据我所知,没有什么是空的。
第一个if语句有效。 当某人从“刻录项目”列表中选择“选择”时,将在“刻录项目”列表下方显示“刻录文件”列表。 (这有效)。 然后,当某人从BurnPiles列表中选择“选择”时,将在其下方显示RequestedBurns列表。 这给了我空错误。 一次它确实工作了,但现在却没有。
我不知道出了什么问题。 我确实知道RequestedBurn表从记录#2开始,但是那与它没有任何关系。 我一直在使用的BurnPile记录已关联RequestedBurns。
//when 'Select' is chosen from the list of burn projects the list of burn piles
//associated with that specific burn project is display below it.
if (burnerID != null)
{
ViewBag.BurnerID = burnerID.Value;
viewModel.BurnPiles = viewModel.BurnProjects.Where(
b => b.BurnerID == burnerID.Value).Single().BurnPiles;
}
//when 'Select' is chosen from the list of burn piles the list of requested
//burns associated with that specific burn pile is displayed below it.
if (burnPileID != null)
{
ViewBag.BurnPilesID = burnPileID.Value;
viewModel.RequestedBurns = viewModel.BurnPiles.Where(
x => x.BurnPilesID == burnPileID).Single().RequestedBurns;
}
viewModel.BurnPiles = viewModel.BurnProjects.Where(
b => b.BurnerID == burnerID.Value).Single().BurnPiles;
可以将viewModel.BurnPiles设置为null。
要么
viewModel.BurnPiles.Where(
x => x.BurnPilesID == burnPileID).Single()
没有返回任何内容,因此当您尝试访问RequestedBurns时,它将引发异常。
SingleOrDefault
也有一个重载 ,您可以在其中进一步简化表达式。 您还可以将其与空条件运算符结合在一起(如果至少使用C#6)。
if (burnerID != null)
{
ViewBag.BurnerID = burnerID.Value;
viewModel.BurnPiles = viewModel.BurnProjects.SingleOrDefault(b => b.BurnerID == burnerID.Value)?.BurnPiles;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.