title: ASP.NET MVC + LINQ exception
body: <p>I've got a ASP.NET MVC application that works great for 99.9% of the time. Once in a blue moon though things go really wrong and I was wondering if anybody could shed some light on what might go wrong here. </p>
  
  <p>The web application is using Linq2SQL and blows up in a controller after the following set of instructions:</p>
  
  <pre><code>const int pageSize = 5;
  var allHeadings = artRepository.FindAllVisibleHeadings();
  var paginatedHeadings = new PaginatedList&lt;Article&gt;(allHeadings, id ?? 0, pageSize);
  </code></pre>
  
  <p>allHeadings contains just an IQueryable list of all visible headings for articles, whereas PaginatedList takes care of taking an appropriate chunk out of this very long list. It's as follows:</p>
  
  <pre><code>public PaginatedList(IQueryable&lt;T&gt; source, int pageIndex, int pageSize)
  {
      PageIndex = pageIndex;
      PageSize = pageSize;
      TotalCount = source.Count();
      TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);
  
      this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
  }
  </code></pre>
  
  <p>It blows up on the <strong>source.Count()</strong> line so when it's counting all the visible articles in the db. Funny thing though is that when I reload the page multiple times I'm getting 2 different kinds of exceptions:</p>
  
  <p>THE FIRST: Sequence contains more than one element</p>
  
  <pre><code>at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
  at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
  at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
  at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
  at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
  at KoscierzynaInfo.Helpers.PaginatedList`1..ctor(IQueryable`1 source, Int32 pageIndex, Int32 pageSize) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Helpers\PaginatedList.cs:line 20
  at KoscierzynaInfo.Controllers.HomeController.Index(Nullable`1 id) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Controllers\HomeController.cs:line 63
  at lambda_method(ExecutionScope , ControllerBase , Object[] )
  at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClassa.&lt;InvokeActionMethodWithFilters&gt;b__7()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClassa.&lt;&gt;c__DisplayClassc.&lt;InvokeActionMethodWithFilters&gt;b__9()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  at System.Web.Mvc.Controller.ExecuteCore()
  at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
  at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>AND THE SECOND TYPE: Index was outside of the bounds of the array</p>
  
  <pre><code>at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
  at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
  at Read_Article(ObjectMaterializer`1 )
  at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
  at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
  at KoscierzynaInfo.Helpers.PaginatedList`1..ctor(IQueryable`1 source, Int32 pageIndex, Int32 pageSize) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Helpers\PaginatedList.cs:line 20
  at KoscierzynaInfo.Controllers.HomeController.Index(Nullable`1 id) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Controllers\HomeController.cs:line 63
  at lambda_method(ExecutionScope , ControllerBase , Object[] )
  at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
  at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClassa.&lt;InvokeActionMethodWithFilters&gt;b__7()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  at System.Web.Mvc.ControllerActionInvoker.&lt;&gt;c__DisplayClassa.&lt;&gt;c__DisplayClassc.&lt;InvokeActionMethodWithFilters&gt;b__9()
  at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
  at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
  at System.Web.Mvc.Controller.ExecuteCore()
  at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
  at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
  at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
  at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
  at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously)
  </code></pre>
  
  <p>The only way I came up with to fix this problem was restarting IIS or recycling the pool. Did this issue happen to any of you before? Where does it come from?! And is there any remedy for that? </p>
  
  <p>I moved the application from IIS7 + SQL Server 2005 to another server with IIS6 + SQLServer 2008 hoping that it would solve the problem, but unfortunately it happened again today, which leads me to believe that this issue is not really system/db dependent.</p>