序列不包含服务器端的元素

本文关键字:元素 服务器端 包含 | 更新日期: 2023-09-27 17:57:15

我正在开发一个 Asp.net 的MVC Web应用程序,并且已经部署到Web服务器几个月了。就在最近,我遇到了一个新的错误,让我感到困惑。发布我的应用程序并加载网站时,我收到以下错误消息:

 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: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace: 

[InvalidOperationException: Sequence contains no elements]
 System.Linq.Enumerable.First(IEnumerable`1 source) +514
System.Linq.Queryable.First(IQueryable`1 source) +330
LabSys.MvcApplication.Application_AuthorizeRequest(Object sender, EventArgs   e) +1261
  System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&completedSynchronously) +165

我很困惑,因为该应用程序在本地主机上完美运行,没有任何问题或错误消息。我还检查了我的数据库,它也已连接到服务器并进行了验证。所以应该没有错误。

提前感谢您的任何帮助!

序列不包含服务器端的元素

签入代码以确保使用 FirstOrDefault() 或 SingleOrDefault() 而不是使用以下任何一种:First()Single()。

当您收到 LINQ 错误"序列不包含任何元素"时,这是 通常是因为您使用的是 First() 或 Single() 命令 比 FirstOrDefault() 和 SingleOrDefault()。

序列不包含元素?