【问题标题】:ASP.NET MVC 1 Linq to SQL ErrorASP.NET MVC 1 Linq to SQL 错误
【发布时间】:2009-12-02 11:23:47
【问题描述】:

我有这个电话:

List<Product> featProducts = 
    productsRepository.Products.Where(x => x.Featured == true).ToList();

它返回以下错误:System.InvalidCastException: Specified cast is not valid.

我觉得这很奇怪,因为我在其他地方也打过类似的电话,而且一切正常。我的应用程序编译没有问题,所以我不明白是什么原因造成的。下面是详细的堆栈跟踪。

任何帮助将不胜感激。

问候,

让-菲利普

“/”应用程序中的服务器错误。指定的演员表无效。

描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 代码。

异常详情: System.InvalidCastException:指定 强制转换无效。

来源错误:

第 50 行:公共 ViewResult Featured() 第 51 行:{ 第 52 行: 列出专长产品 = productsRepository.Products.Where(x => x.Featured == true).ToList();第 53 行: 第 54 行:foreach (var p in 壮举产品)

源文件:G:\My Webs\clients\CBP\CBP APP\WebUI\Controllers\ProductsController.cs 线路:52

堆栈跟踪:

[InvalidCastException:指定的强制转换无效。]
System.Data.SqlClient.SqlBuffer.get_Single() +55
System.Data.SqlClient.SqlDataReader.GetFloat(Int32 i) +38
Read_Product(ObjectMaterializer1 ) +825 System.Data.Linq.SqlClient.ObjectReader2.MoveNext() +29 System.Collections.Generic.List1..ctor(IEnumerable1 收藏)+7667540
System.Linq.Enumerable.ToList(IEnumerable1 source) +61
WebUI.Controllers.ProductsController.Featured() in G:\My Webs\clients\CBP\CBP APP\WebUI\Controllers\ProductsController.cs:52 lambda_method(ExecutionScope , ControllerBase , Object[] ) +39
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +17
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2 参数)+178
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext 控制器上下文、动作描述符 actionDescriptor,IDictionary2 parameters) +24
System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
1 续)+254 System.Web.Mvc.c__DisplayClassc.b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext 控制器上下文,IList1 filters, ActionDescriptor actionDescriptor, IDictionary2 个参数)+192
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext 控制器上下文,字符串动作名称) +399 System.Web.Mvc.Controller.ExecuteCore() +126 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +27
System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +7
System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext) +151
Microsoft.Web.Mvc.ViewExtensions.RenderRoute(HtmlHelper 助手,RouteValueDictionary 路线值)+214
Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper 助手,字符串 actionName,字符串 控制器名称,路由值字典 路线值)+315
Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper 助手,字符串 actionName,字符串 控制器名称)+10
ASP.views_pages_home_ascx.__Render__control1(HtmlTextWriter __w,控制参数容器)在 g:\My Webs\clients\CBP\CBP APP\WebUI\Views\Pages\home.ascx:12
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter 作家,ICollection 儿童)+256
System.Web.UI.Control.RenderChildren(HtmlTextWriter 作家)+19
System.Web.UI.Control.Render(HtmlTextWriter 作家)+10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 作家,ControlAdapter 适配器)+27
System.Web.UI.Control.RenderControl(HtmlTextWriter 作家,ControlAdapter 适配器)+99
System.Web.UI.Control.RenderControl(HtmlTextWriter 作家)+25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter 作家,ICollection 儿童)+134
System.Web.UI.Control.RenderChildren(HtmlTextWriter 作家)+19
System.Web.UI.Page.Render(HtmlTextWriter 作家)+29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter 作家)+59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter 作家,ControlAdapter 适配器)+27
System.Web.UI.Control.RenderControl(HtmlTextWriter 作家,ControlAdapter 适配器)+99
System.Web.UI.Control.RenderControl(HtmlTextWriter 作家)+25
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint) +1266

版本信息:Microsoft .NET 框架版本:2.0.50727.4200; ASP.NET 版本:2.0.50727.4016

【问题讨论】:

    标签: asp.net-mvc linq


    【解决方案1】:

    我找到了问题。这与我的查询无关。

    它在产品类中。我试图将 sql server 浮点数与 .net 浮点数匹配。它不喜欢那样。使用 .net double 解决了这个问题。

    【讨论】:

      【解决方案2】:

      这个怎么样?

      List<Product> featProducts = productsRepository.Products.Where(x => x.Featured == true).ToList<Product>();
      

      【讨论】:

      • 当然。我不敢相信我没有看到。你知道当你多次阅读同一行时。非常感谢。让-菲利普
      • 我有点太兴奋了。对不起。它仍然出错。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多