title: LINQ: Specified cast is not valid VB.NET, MVC3 Razor
body: <p>this was working yesterday, then today after a bunch of errors i corrected(that seemed to be like the controller had been reverted to an earlier state), i get "Specified cast is not valid." when comparing some fairly direct values. code that generates the error:  </p>
  
  <pre><code>Dim AssignedRequestIDs = From x In _PITcontext.Sessions Where x.Request_ID &gt; 0 Select     x.Request_ID
  Dim result = From r In _PITcontext.Requests Where (Not AssignedRequestIDs.Contains(r.ID)) And (r.NotActive = False) Select r Order By r.NeededByDate Ascending
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>error is on the second line</p>
  
  <p>and the data types are:<br>
   <code>x.request_ID Integer</code> (shouldn't ever be null)<br>
   <code>r.ID Integer</code> (not Null)<br>
   <code>r.NotActive Boolean</code> (not Null)<br>
   <code>r.neededbydate System.Nullable(Of Date)</code> obviously nullable<br>
  corresponding values in the database have matched datatypes. what could be causing this, and why in the heck isn't there a more verbose error? maybe kick back a column name or between which two variables the conflict occurred... (that'd be too easy i guess)<br>
  stack trace is :<br>
      <code>[InvalidCastException: Specified cast is not valid.]</code><br>
      <code>System.Data.SqlClient.SqlBuffer.get_Int32() +6712156</code><br>
      <code>Read_Request(ObjectMaterializer</code>1 ) +6049<code>
  </code>System.Data.Linq.SqlClient.ObjectReader<code>2.MoveNext() +42</code><br>
      <code>System.Linq.WhereSelectEnumerableIterator</code>2.MoveNext() +165<code>
  </code>System.Linq.Buffer<code>1..ctor(IEnumerable</code>1 source) +520<code>
  </code>System.Linq.d__0.MoveNext() +145<code>
  </code>System.Linq.Enumerable.Count(IEnumerable<code>1 source) +267</code><br>
      <code>ASP._Page_Views_Shared__YourRequests_vbhtml.Execute() in C:\inetpub\wwwroot\PIT\Views\Shared\_YourRequests.vbhtml:17</code>
      <code>System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280</code><br>
      <code>System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104</code><br>
      <code>System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +173</code><br>
      <code>System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378</code><br>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +33</code><br>
      <code>System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func</code>1 continuation) +727696<code>
  </code>System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList<code>1 filters, ActionResult actionResult) +265</code><br>
      <code>System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +727652</code><br>
      <code>System.Web.Mvc.Controller.ExecuteCore() +159</code><br>
      <code>System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334</code><br>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +62</code>
      <code>System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +15</code>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +52</code>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClass4.&lt;Wrap&gt;b__3() +15</code>
      <code>System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func</code>1 func) +41<code>
  </code>System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer,     Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath,     String physPath, Exception error, String queryStringOverride) +1411`  </p>
  
  <pre><code>`[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]`
  </code></pre>
  
  <p><code>System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2819</code>
     <code>System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +275</code>
     <code>System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +94</code>
     <code>System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +828</code>
     <code>System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) +123</code>
     <code>ASP._Page_Views_Home_Index_vbhtml.Execute() in C:\inetpub\wwwroot\PIT\Views\Home\Index.vbhtml:44</code>
     <code>System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280</code>
     <code>System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +104</code>
     <code>System.Web.WebPages.StartPage.ExecutePageHierarchy() +143</code>
     <code>System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +157</code>
     <code>System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +378</code>
     <code>System.Web.Mvc.&lt;&gt;c__DisplayClass1c.&lt;InvokeActionResultWithFilters&gt;b__19() +33</code>
     <code>System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func</code>1 continuation) +727696<code>
  </code>System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList<code>1 filters, ActionResult actionResult) +265</code>
     <code>System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +727652</code>
      <code>System.Web.Mvc.Controller.ExecuteCore() +159</code><br>
      <code>System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +334</code><br>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClassb.&lt;BeginProcessRequest&gt;b__5() +62</code><br>
      <code>System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +15</code><br>
      <code>System.Web.Mvc.&lt;&gt;c__DisplayClasse.&lt;EndProcessRequest&gt;b__d() +52</code><br>
      <code>System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606</code><br>
      <code>System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +288</code></p>