C# 处理“序列没有元素”异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11801769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 19:19:22  来源:igfitidea点击:

Handling 'Sequence has no elements' Exception

c#.netlinqentity-frameworkado.net

提问by Arrow

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

我正在更新购物车中的数量,但它抛出了一个 Sequence 没有元素的异常。

And I don't know what that even means. At first I thought that maybe there was a null value being passed somewhere, but that isn't the case, as I've checked that:

我什至不知道这意味着什么。起初我认为可能在某处传递了一个空值,但事实并非如此,因为我已经检查过:

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();

序列不包含任何元素 描述:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。

异常详细信息:System.InvalidOperationException:序列不包含任何元素

源错误:

第 35 行:var uid = WebSecurity.CurrentUserId; 第 36 行:var newqty = Request.Form["Quantity"]; 第 37 行:
OModel.Cart c =(来自 database.Carts 第 38 行:
其中 item.UserId == uid && item.PartNumber == pnumber && item.OrderId == oid 第 39 行:选择 item).First();

Any ideas what could be causing this?

任何想法可能导致这种情况?

采纳答案by Varius

First()is causing this if your select returns 0 rows. You either have to catch that exception, or use FirstOrDefault()which will return null in case of no elements.

First()如果您的选择返回 0 行,则会导致这种情况。您要么必须捕获该异常,要么使用FirstOrDefault()which 在没有元素的情况下将返回 null。

回答by c0deNinja

Instead of .First()change it to .FirstOrDefault()

而不是.First()将其更改为.FirstOrDefault()

回答by Marty Dill

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

您正在使用 linq 的 First() 方法,根据文档,如果您在空集合上调用它,则会抛出 InvalidOperationException 。

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.

如果您希望查询的结果有时为空,您可能希望使用 FirstOrDefault(),如果集合为空,它将返回 null,而不是抛出异常。

回答by user1012506

The value is null, you have to check why... (in addition to the implementation of the solutions proposed here)

该值为空,你必须检查为什么...(除了这里提出的解决方案的实现)

Check the hardware Connections.

检查硬件连接。

回答by ransems

Part of the answer to 'handle' the 'Sequence has no elements' Exception in VBis to test for empty

VB 中“处理”“序列没有元素”异常的部分答案是测试是否为空

If Not (myMap Is Nothing) Then
' execute code
End if

Where MyMap is the sequence queried returning empty/null. FYI

其中 MyMap 是查询返回空/空的序列。供参考