2009-06-20 17 views
1

この変換タイプのエラーを解決するにはどうすればよいですか?このタイプのエラーを解決する方法は?

が暗黙的にタイプを変換できません

'System.Collections.Generic.List' に 'エラー2

System.Collections.Generic.List

暗黙的に変換できません。タイプ ' System.Collections.Generic.List<AnonymousType#1> System.Collections.Generic.List<Product> ' に' ' D:\フォーチュン\にApp_Code \ BL \ StoreController.cs 204 45 D:\フォーチュン\

public static List<Product> GetProductsByCategoryID(int productCategoryId) 
{ 
    FortuneDataContext db = SiteController.GetNewFortuneDataContext(); 
    List<Product> prods = (from p in db.Products 
         join pc in db.ProductCategories 
         on p.ProductCategoryId equals pc.ProductCategoryId 
         where pc.ParentProductCategoryId == productCategoryId 
         select new 
         { 
          p.ProductId, 
          p.ProductCategoryId, 
          pc.ParentProductCategoryId,    
          ProductName = p.Name, 
          Category = pc.Name, 
          p.Price, 
          p.ProductYear 
          }).ToList(); 
    return prods; 

} 

答えて

3

変更は、すべての製品のフィールドを抽出するために

をしたい場合は、エラーがあなたのリスト項目は、製品

3

ないという問題があなたのことです

select new Product {ProductId = p.ProductID 
         ...}).ToList(); 

または

select p ... 

に選択しますクエリが製品を返すことはありません。 select new {}を使用して新しい型を投影しているため、匿名型が返されています。

select new { ... } 

あなたのオブジェクトにカテゴリ名を持つ新しいタイプを作成して、それを返すようにする方法を変更したり、LINQ to SQLのを使用している場合は、product.category.nameような何かを行うことができるかもしれないどちらか返品された商品について

List<Product> prods = (from p in db.Products 
          join pc in db.ProductCategories on p.ProductCategoryId equals pc.ProductCategoryId 
          where pc.ParentProductCategoryId == productCategoryId 
          select p).ToList<Product>(); 
関連する問題