我有一些代碼是這樣的:由於物化值爲空,因此值類型爲「Int32」的轉換失敗。請幫助我如何固定它
public int GetCount()
{
// Get the count of each item in the cart and sum them up
var s = BikesDB.ShoppingCartItems.Where(cart => cart.ShoppingCartID == ShoppingCartID1).Select(cart => cart.Quantity).Sum(cart => cart);
// Return 0 if all entries are null
return s;
}
在這裏,我得到了什麼,當我調試:
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
我非常欣賞從你的任何問題,非常感謝!
嘗試刪除總和部分,看看它是否工作,猜測其中一個項目是空的,並試圖將其總和試圖轉換爲int –