2009-12-02 36 views
1

我有这样的电话:ASP.NET MVC 1 LINQ到SQL错误

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

它返回以下错误:System.InvalidCastException:指定的转换无效。

我觉得很奇怪,因为我在其他地方打类似的电话,它都可以正常工作。我的应用程序编译没有问题,所以我不明白可能是什么原因造成的。以下是详细的堆栈跟踪。

任何帮助将不胜感激。

问候,

让 - 菲利普·

Server Error in '/' Application. Specified cast is not valid.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Source Error:

Line 50: public ViewResult Featured() Line 51: { Line 52: List featProducts = productsRepository.Products.Where(x => x.Featured == true).ToList(); Line 53: Line 54: foreach (var p in featProducts)

Source File: G:\My Webs\clients\CBP\CBP APP\WebUI\Controllers\ProductsController.cs Line: 52

Stack Trace:

[InvalidCastException: Specified cast is not valid.]
System.Data.SqlClient.SqlBuffer.get_Single() +55
System.Data.SqlClient.SqlDataReader.GetFloat(Int32 i) +38
Read_Product(ObjectMaterializer 1) +825 System.Data.Linq.SqlClient.ObjectReader 2.MoveNext() +29 System.Collections.Generic.List 1..ctor(IEnumerable 1 collection) +7667540
System.Linq.Enumerable.ToList(IEnumerable 1 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 parameters) +178
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2 parameters) +24
System.Web.Mvc.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7() +52 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func
1 continuation) +254 System.Web.Mvc.<>c__DisplayClassc.b__9() +19 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList 1 filters, ActionDescriptor actionDescriptor, IDictionary 2 parameters) +192
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +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 helper, RouteValueDictionary routeValues) +214
Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper helper, String actionName, String controllerName, RouteValueDictionary routeValues) +315
Microsoft.Web.Mvc.ViewExtensions.RenderAction(HtmlHelper helper, String actionName, String controllerName) +10
ASP.views_pages_home_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in g:\My Webs\clients\CBP\CBP APP\WebUI\Views\Pages\home.ascx:12
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Control.Render(HtmlTextWriter writer) +10
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +134
System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19
System.Web.UI.Page.Render(HtmlTextWriter writer) +29
System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +59
System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27
System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99
System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266

Version Information: Microsoft .NET Framework Version:2.0.50727.4200; ASP.NET Version:2.0.50727.4016

回答

3

我发现了这个问题。这与我的查询无关。

这是在产品类。我试图用.net浮点数来匹配一个sql server float。它不喜欢那样。使用.net双重解决了这个问题。

0

这件怎么样?

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

Course。 我不相信我没有看到。 你知道你何时多次阅读同一行。 非常感谢。 让 - 菲利普 – user208250 2009-12-02 11:56:01

+0

那里我有点太激动了。抱歉。 它仍然是错误的。 – user208250 2009-12-02 12:01:06

+0

还是一样的错误? – 2009-12-02 12:30:08