[英]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.