2009-12-02 82 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