Colin O'Dell
Colin O'Dell

Reputation: 8647

Strange LINQ Exception

I have a similar issue to these posts: I'm posting separately to provide additional information about the issue in my case, along with steps I've taken to debug it.

Basically the site works completely fine for 12+ hours and then suddenly stops working. I'll start getting strange exceptions thrown in LINQ queries that have 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 remade my DBML by dragging the tables directly from the DB in Server Explorer. Comparing the old and the new in Git, I found a few fields that differed:

Some observations/notes:

The exception always seems to be thrown at this line:

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

Any thoughts on why this happening and how to resolve the issue?

EDIT 1: I have added the relevant code above the corresponding stack traces. The exception gets throw when the last line of code executes.

Here are a couple stack traces I was able to copypaste before restarting the site:

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

There was also an InvalidCastException thrown on one particular page. Updating the dbml did not seem to fix this, but restarting the site did.

Upvotes: 3

Views: 2652

Answers (2)

Stephen Kiningham
Stephen Kiningham

Reputation: 504

Looks like in your LINQ you're making a call to .Single() (or similar) which throws that exception if the IQueryable collection holds more than one element.

Next time, please post your code and not just your stack trace.

Upvotes: 1

Jeff
Jeff

Reputation: 36553

Are you disposing your DataContext on each request (you should be)? Or do you have a single DataContext that lives forever (it sounds like this may be the case)?

It sounds like it could be a threading problem.

Upvotes: 2

Related Questions