2012-09-18 34 views
1

查詢嵌套集合時,我有一個類似的問題,因爲在這個問題:linq question: querying nested collectionsInvalidCastException的使用LINQ

我有一個ICollection<Category>一個產品,我想通過ID的類別。我通過一個IProductsRepository。

我已經嘗試了products.SelectMany(p => p.Categories).Where(c => c.CategoryID == categoryId)products.SelectMany(p => p.Categories).First(c => c.CategoryID == categoryId)以及我嘗試過的許多其他變體。但是我沒有得到它的工作。

在運行時我收到InvalidCastException。 Category.CategoryID和categoryId都是int。

創建一個ICategoriesRepository會更好嗎?也許它也有性能優勢?我明顯對LINQ很陌生,因此不確定如何正確執行操作。

EDIT(代碼示例):

public interface IProductsRepository 
{ 
    IQueryable<Product> Products { get; } 
} 

public class ProductsController : Controller 
{ 
    public int PageSize = 4; 
    private IProductsRepository productsRepository; 

    public ProductsController(IProductsRepository productsRepository) 
    { 
     this.productsRepository = productsRepository; 
    } 

    public ViewResult ListById(int categoryId, int page = 1) 
    { 
     Category cat; 
     // What do I need here to get the Category with the categoryId regardless of which Product it is connected to? 
     return List(cat, page); 
    } 

    private ViewResult List(Category category, int page = 1) { //this code works } 
} 

[Table(Name = "products")] 
public class Product 
{ 
    [HiddenInput(DisplayValue = false)] 
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
    public int ProductID { get; set; } 

    [Column] 
    public string Name { get; set; } 

    [Column(Name = "info")] 
    public string Description { get; set; } 

    public float LowestPrice 
    { 
     get { return (from product in ProductSubs select product.Price).Min(); } 
    } 

    private EntitySet<Category> _Categories = new EntitySet<Category>(); 
    [System.Data.Linq.Mapping.Association(Storage = "_Categories", OtherKey = "CategoryID")] 
    public ICollection<Category> Categories 
    { 
     get { return _Categories; } 
     set { _Categories.Assign(value); } 
    } 
} 

[Table(Name = "products_types")] 
public class Category 
{ 
    [HiddenInput(DisplayValue = false)] 
    [Column(Name = "id", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)] 
    public int CategoryID { get; set; } 

    [Column(Name = "id")] // Temp solution, real name is localized in a separate table 
    public string Name { get; set; } 

    private EntitySet<Product> _Products = new EntitySet<Product>(); 
    [System.Data.Linq.Mapping.Association(Storage = "_Products", OtherKey = "ProductID")] 
    public ICollection<Product> Products 
    { 
     get { return _Products; } 
     set { _Products.Assign(value); } 
    } 
} 

編輯:

我試圖做到這一點通過ICategoriesRepository代替,但我得到了同樣的錯誤。這是一個完整的堆棧跟蹤:

[InvalidCastException的:指定的轉換是無效] System.Data.Linq.SqlClient.SqlProvider.Execute(表達式查詢,QueryInfo queryInfo,IObjectReaderFactory廠,對象[] parentArgs,對象[ ] userArgs,ICompiledSubQuery [] subQueries,Object lastResult)+1191 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query,QueryInfo [] queryInfos,IObjectReaderFactory factory,Object [] userArguments,ICompiledSubQuery [] subQueries)+118 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(表達式查詢)+342 System.Data.Linq.Table 1.System.Linq.IQueryProvider.Execute(Expression expression) +58 System.Linq.Queryable.First(IQueryable 1來源,表達式1 predicate) +287 MaxFPS.WebUI.Controllers.ProductsController.ListById(Int32 categoryId, Int32 page) in d:\Filer\Documents\Dropbox\ZkilfinG\webbutveckling\MaxFPS\VS Projects\MaxFPS\MaxFPS\Controllers\ProductsController.cs:26 lambda_method(Closure , ControllerBase , Object[]) +140 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2參數)+182 System.Web .Mvc.Cont rollerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary的2 parameters) +27 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 System.Web.Mvc.Async.<>c__DisplayClass8 1.b__7(IAsyncResult的_)10 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult 1.End()50 System.Web.Mvc.Async.AsyncControllerActionInvoker。 EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34 System.Web.Mvc.Async。 <> c__DisplayClass2a.b__20()+24 System.Web.Mvc.Async。 <> c__DisplayClass25.b__22(IAsyncResult的asyncResult)99 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult 1.End()55 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)39 的System.Web。 Mvc.Async。 <> c__DisplayClass4.b__3(IAsyncResult的AR)23 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +55 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult 1.End()55 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult)31 的System.Web。 Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的結果)9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()9629708 System.Web.HttpApplication.ExecuteStep(IExecutionStep步驟,布爾& completedSynchronously)155

編輯:

當類標識符是已設定爲一個id是不在數據庫中,我得到一個空序列。這對我來說表明比較代碼是正確的,但我不明白爲什麼或什麼是無效投射。

[InvalidOperationException異常:序列不包含任何元素] System.Data.Linq.SqlClient.SqlProvider.Execute(表達式查詢,QueryInfo queryInfo,IObjectReaderFactory工廠,對象[] parentArgs,對象[] userArgs,ICompiledSubQuery []子查詢,對象+1191 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(表達式查詢,QueryInfo [] queryInfos,IObjectReaderFactory工廠,Object [] userArguments,ICompiledSubQuery []子查詢)+118 System.Data.Linq.SqlClient.SqlProvider .System.Data.Linq.Provider.IProvider.Execute(表達式查詢)+342 System.Data.Linq.Table 1.System.Linq.IQueryProvider.Execute(Expression expression) +58 System.Linq.Queryable.First(IQueryable 1 source,Expression 1 predicate) +287 MaxFPS.WebUI.Controllers.ProductsController.ListById(Int32 categoryId, Int32 page) in d:\Filer\Documents\Dropbox\ZkilfinG\webbutveckling\MaxFPS\VS Projects\MaxFPS\MaxFPS\Controllers\ProductsController.cs:28 lambda_method(Closure , ControllerBase , Object[]) +140 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 parameters)+182 System.Web.Mvc.ControllerActionInvo ker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary的2 parameters) +27 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 System.Web.Mvc.Async.<>c__DisplayClass8 1.b__7(IAsyncResult的_)10 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32 System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult 1.End()50 System.Web.Mvc.Async.AsyncControllerActionInvoker。 EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34 System.Web.Mvc.Async。 <> c__DisplayClass2a.b__20()+24 System.Web.Mvc.Async。 <> c__DisplayClass25.b__22(IAsyncResult的asyncResult)99 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult 1.End()55 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult的asyncResult)39 的System.Web。 Mvc.Async。 <> c__DisplayClass4.b__3(IAsyncResult的AR)23 System.Web.Mvc.Async.WrappedAsyncResult 1.End() +55 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult 1.End()55 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult的asyncResult)31 的System.Web。 Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult的結果)9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()9629708 System.Web.HttpApplication.ExecuteStep(IExecutionStep步驟,布爾& completedsynchronously

+0

您能否提供有關異常(堆棧跟蹤)的更多詳細信息? – wlabaj

+1

你想要什麼:從一個產品中選擇一個類別或一個類別的產品?爲什麼不var res = products.Categories.Where(c => c.CategoryID == categoryId).FirstOdDefault(); – Cybermaxs

+0

請提供更完整的代碼示例 – jeroenh

回答

0

這個錯誤很簡單,一旦我想出來在我真正的數據庫中,我有一個LocalizedCategory來獲取本地化的類別名稱,當我想我可以跳過使用它簡化事情。所以我用這段代碼給Name賦值:

[Column(Name = "id")] 
    public string Name { get; set; } 

但是,由於id在數據庫中是int,所以我得到一個異常。我現在要儘快讓它與LocalizedCategory一起工作,儘管我遇到了另一個問題:Why do I get different values from my EntitySet depending on how I LINQ to it?