来自物化系统.Int16'的指定演员表。输入' System.String'类型无效

时间:2015-01-21 17:47:13

标签: c# asp.net-mvc linq entity-framework

以下代码引发了此错误:

  

从物化“System.Int16”类型到“System.String”类型的指定强制转换无效

当我删除ToList()时,它不会出现此错误。

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();

下面给出的堆栈跟踪:

[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

1 个答案:

答案 0 :(得分:1)

DB和EF之间ItemNumber的差异是原因。

通过查看您指定的所有值,我从您的陈述中找到了一些线索。

  • OrderDate必须是约会,所以没关系。
  • DateIssued(与上述相同)
  • SerialNumber是你在评论中提到的varchar,所以它是 细
  • CostPerUnit是十进制的,所以没关系。

因此只留下一个可能的选项 - ItemNumber :)希望这有帮助。