我正在更新購物車中的數量,但它拋出的序列中沒有元素的例外。處理「序列沒有元素」異常
我甚至不知道這意味着什麼。起初我想,也許有什麼地方被傳遞空值,但事實並非如此,因爲我已經檢查了:
Sequence contains no elements Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Sequence contains no elements
Source Error:
Line 35: var uid = WebSecurity.CurrentUserId; Line 36: var newqty = Request.Form["Quantity"]; Line 37:
OModel.Cart c = (from item in database.Carts Line 38:
where item.UserId == uid && item.PartNumber == pnumber && item.OrderId == oid Line 39: select item).First();
任何想法可能是造成這個?
您可能想要閱讀http://stackoverflow.com/questions/1024559/when-to-use-first-and-when-to-use-firstordefault-with-linq/1024577#1024577 – driis 2012-08-03 19:07:24
@driss我'當我想要學習如何更新時,我發現了一個博客(和我學會添加和刪除的博客一樣) - 並且他使用First而不是FirstOrDefault(),而使用FirstOrDefault所以我用它來代替。但是FirstOrDefault()會拋出另一個異常。所以在這種情況下,我不確定應該使用哪一個,或者甚至是否重要。 – Arrow 2012-08-03 19:13:54