Handling 'Sequence has no elements' Exception

前端 未结 6 1289
天涯浪人
天涯浪人 2020-12-09 01:25

I am updating a quantity in my cart, but it is throwing a Sequence has no elements\' exception.

And I don\'t know what that even means. At first I thought that maybe

6条回答
  •  醉酒成梦
    2020-12-09 01:59

    You are using linq's First() method, which as per the documentation throws an InvalidOperationException if you are calling it on an empty collection.

    If you expect the result of your query to be empty sometimes, you likely want to use FirstOrDefault(), which will return null if the collection is empty, instead of throwing an exception.

提交回复
热议问题