IEnumerable<ColorRow> result = (from a in Fruit.Apple.Colors where (a.color == "red") select a);
如果我嘗試使用result.Any()或Count()它會拋出異常,當沒有結果返回時,我明白上面的ColorRow將爲null,但我該如何檢查?IEnumerable任何或計數拋出異常
將更改上面的var結果幫助它等於Colrow result = new ColorRow ??
不知道如何處理IEnumerable上面的空值,爲空。
我目前正在嘗試使用,但看起來並不乾淨。
try
{
result.FirstOrDefault().Color= "pink";
return true;
}
catch
{
return false;
}
'null'!=空!請顯示引發異常的實際代碼,並告訴我們它是什麼異常 - 以及它的錯誤消息是什麼。 – 2013-02-25 10:51:13
@ Daniel:Result.FirstOrDefault()拋出NullreferenceException:未將對象引用設置爲實例。 – Gauls 2013-02-25 15:00:08