Strange LINQ exception

I have a similar problem with these messages : I send separately to provide additional information about the problem in my case, as well as the steps that I took to debug it.

Basically, the site works completely fine for 12 hours, and then suddenly stops working. I will start to get weird exceptions caused by LINQ queries that previously worked fine.

I have included stack traces at the bottom of this post.

Taking the advice found on similar SO posts, I deleted and redid my DBML by dragging tables directly from the database into Server Explorer. Comparing the old and the new in Git, I found several fields that were different:

  • One instance of varchar (255) is set as nchar (10) in dbml
  • Two instances of varchar (255) that previously did not have NOT NULLin dbml
  • The int (4) field was not previously NOT NULLin dbml

Some observations / notes:

  • The site has been working at 100% for some time, and then it just starts throwing these errors.
  • I can fix this temporarily by restarting the site in IIS or by restarting the MSSQL server, but the problem usually returns the next day.
  • I cannot figure out how to replicate it manually
  • For some pages, I can reload them several times and get different but similar exceptions
    • I can get InvalidOperationExceptionin the line that calls .Count(), then reload it and get IndexOutOfRangeExcpetionin the line that calls .Any()or.First()
    • , , -
  • , DataContext - ?
  • , db, , , db . , ?

:

System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539

, ?

EDIT 1: . , .

, :

IEnumerable<Chapter> toc = (from c in db.Chapters
    where c.Sections.Any()
    orderby c.ChapterNumber ascending
    select c);
return toc.Count();

[InvalidOperationException: Sequence contains more than one element]
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +41
   System.Linq.Queryable.Count(IQueryable`1 source) +352
   MvcPaging.PagedList`1.Initialize(IQueryable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:63
   MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:16
   MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:10
   MvcPaging.PagingExtensions.ToPagedList(IEnumerable`1 source, Int32 pageIndex, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagingExtensions.cs:77
   MyNamespace.Controllers.ChaptersController.TableOfContents(Nullable`1 page) in C:\path\to\my\project\Controllers\ChaptersController.cs:73
   lambda_method(Closure , ControllerBase , Object[] ) +118
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +37




var contentTop = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-top");
var contentBot = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-bottom");
ViewData["ContentAboveTheBox"] = (contentTop.Any() ? contentTop.First().HTML : String.Empty);



[IndexOutOfRangeException: Index was outside the bounds of the array.]
   System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539
   System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207
   System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500
   System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +50
   System.Linq.Queryable.First(IQueryable`1 source) +383
   MyNamespace.Controllers.AccountController.SetLoginContentAreas() in C:\path\to\my\project\Controllers\AccountController.cs:112
   MyNamespace.Controllers.AccountController.LogIn(String userName, String password, Nullable`1 rememberMe, String returnUrl, Nullable`1 IsEnc) in C:\path\to\my\project\Controllers\AccountController.cs:122
   MyNamespace.Controllers.AccountController.LoginPassThru() in C:\path\to\my\project\Controllers\AccountController.cs:82
   lambda_method(Closure , ControllerBase , Object[] ) +79
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
   System.Web.Mvc.Controller.ExecuteCore() +159
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

InvalidCastException. dbml, , , .

+3
2

DataContext ( )? DataContext, (, )?

, .

+2

, LINQ .Single() ( ), , IQueryable .

, .

+1

All Articles