C# – The specified cast from a materialized ‘System.Int16’ type to the ‘System.String’ type is not valid

asp.net-mvccentity-frameworklinq

The following code is throwing this error:

The specified cast from a materialized 'System.Int16' type to the 'System.String' type is not valid

When I removed ToList() it doesn't give this error.

var rmas = (
    from o in db.Orders
    join od in db.OrderDetails on o.OrderNumber equals od.OrderNumber
    join r in db.RMAs on o.OrderNumber equals r.OrderNumber
    join rd in db.RMADetails on r.RMANumber equals rd.RMANumber
    where (rd.WrongSKU == od.SKU)
    join i in db.Inventory on od.SKU equals i.LocalSKU into grp
    from g in grp.DefaultIfEmpty()
    select new
    {
        r.RMANumber,
        r.Reason,
        o.Name,
        o.Company,
        o.Address,
        o.Address2,
        o.City,
        o.State,
        o.Country,
        o.Email,
        o.Zip,
        o.Phone,
        o.ShipName,
        o.ShipCompany,
        o.ShipAddress,
        o.ShipAddress2,
        o.ShipCity,
        o.ShipCountry,
        o.ShipState,
        o.ShipPhone,
        o.ShipZip,
        o.OrderNumber,
        o.ShippingTotal,
        o.BalanceDue,
        r.Status,
        OrderDate = o.OrderDate,
        DateIssued = r.DateIssued,
        SerialNumbers = rd.SerialNumbers ?? "",
        o.SourceOrderID,
        od.SKU,
        od.ItemNumber,
        QTYOrdered = od.QuantityOrdered,
        od.QuantityReturned,
        rd.QuantityAuthorized,
        RMA_ItemNumber1 = rd.ItemNumber,
        rd.Price,
        rd.WrongSKU,
        g.Text1,
        CostPerUnit = od.CostPerUnit == null ? 0.0M : od.CostPerUnit
    }
).Take(50).ToList();

Stack Trace given below:

[InvalidOperationException: The specified cast from a materialized 'System.Int16' type to the 'System.String' type is not valid.]
   System.Data.Common.Internal.Materialization.ErrorHandlingValueReader`1.GetValue(DbDataReader reader, Int32 ordinal) +337
   System.Data.Common.Internal.Materialization.Shaper.GetColumnValueWithErrorHandling(Int32 ordinal) +57
   lambda_method(Closure , Shaper ) +772
   System.Data.Common.Internal.Materialization.Coordinator`1.ReadNextElement(Shaper shaper) +170
   System.Data.Common.Internal.Materialization.SimpleEnumerator.MoveNext() +84
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +327
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +58
   XME.Controllers.ListController.Index() in C:\User\Me\XMENew\XME\XME\Controllers\ListController.cs:33
   lambda_method(Closure , ControllerBase , Object[] ) +96
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +51
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +409
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +52
   System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +45
   System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +64
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +39
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +128
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +94
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +44
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +112
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +325
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +39
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +128
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +94
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +45
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +50
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +186
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +128
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +51
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +140
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +47
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +140
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +48
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +140
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +54
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +8862381
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +184

Best Answer

The differences of ItemNumber between DB and EF is the cause.

I figured it out with some clues from your statement by looking at all the values that you assigned.

  • OrderDatemust be a date, so it's fine.
  • DateIssued (same as above)
  • SerialNumber was a varchar as you mentioned in the comment, so it's fine
  • CostPerUnit is decimal so it's fine.

So that leaves only one possible option - ItemNumber :) Hope this helps.

Related Topic