该序列不包含任何元素

时间:2020-01-07 12:08:45

标签: sql-server asp.net-mvc entity-framework

我收到此错误,我知道有很多与此错误有关的问题,但我相信这是另一种情况。另外,我尝试从SQL Server数据库中清除一些记录,因此我认为可能是不一致数据库,但问题是我不确定如何解决它?任何想法将不胜感激。谢谢

 Server Error in '/' Application.

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

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

源错误:

Line 121:                                    </tr>
Line 122:                                </thead>
Line 123:  ->The error is here!<--       @foreach (var item in Model.UserAbsences.OrderByDescending(x => x.UserAbsenceActions.Max(y => y.CreatedAt)).Where(c => c.Status.Equals(1) || c.Status.Equals(2)))
Line 124:                                {
Line 125:                                    <tbody>

Source File: Views\Home\Index.cshtml    Line: 123

Stack Trace:


[InvalidOperationException: Sequence contains no elements]
   System.Linq.Enumerable.Max(IEnumerable`1 source) +324
   System.Linq.Enumerable.Max(IEnumerable`1 source, Func`2 selector) +60
   ASP.<>c.<Execute>b__3_6(UserAbsenceVm x) in \Views\Home\Index.cshtml:123
   System.Linq.EnumerableSorter`2.ComputeKeys(TElement[] elements, Int32 count) +81
   System.Linq.EnumerableSorter`1.Sort(TElement[] elements, Int32 count) +21
   System.Linq.<GetEnumerator>d__1.MoveNext() +232
   System.Linq.WhereEnumerableIterator`1.MoveNext() +174
   ASP._Page_Views_Home_Index_cshtml.Execute() in Views\Home\Index.cshtml:123
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +197
   System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +105
   System.Web.WebPages.StartPage.RunPage() +17
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +73
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
   System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +235
   System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer) +107
   System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +291
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +56
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +420
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +52
   System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +198
   System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
   System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
   System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
   System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
   System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
   System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9849993
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +50
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +163

2 个答案:

答案 0 :(得分:1)

您可以如下所示处理null

@if (Model != null)
{
  //Your original code here as in line number 123.
}

答案 1 :(得分:0)

您可以包装for循环来检查要循环的内容是否确实存在。我在这里假设Model != null

@{
    if(Model.UserAbsences.Any()) {
        foreach (var item in Model.UserAbsences.OrderByDescending(x => x.UserAbsenceActions.Max(y => y.CreatedAt)).Where(c => c.Status.Equals(1) || c.Status.Equals(2))) {
             //...do stuff...
        }
    }
}