无法将类型'System.Collections.Generic.List'隐式转换为'System.Collections.Generic.List

2 asp.net

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List

Error 2 Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<Product>' D:\Fortune\App_Code\BL\StoreController.cs 204 45 D:\Fortune\

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;

}
Run Code Online (Sandbox Code Playgroud)

我想要选择新的{...}中的所有字段,我喜欢这样的选择,但所有字段都不在Product表中.....

          select new Product
                    {
                       p.ProductId,
                       p.ProductCategoryId,
                       pc.ParentProductCategoryId,               
                       ProductName = p.Name,
                       Category = pc.Name,
                       p.Price,
                       p.ProductYear
                       }).ToList();  
Run Code Online (Sandbox Code Playgroud)

我怎么写这个陈述?请.....

小智 6

而不是选择新的,使用选择新产品.它将返回Product类型列表:

...         
select new Product                   
{
    p.ProductId,
    p.ProductCategoryId,
    pc.ParentProductCategoryId,                                          
    ProductName = p.Name,                           
    Category = pc.Name,                           
    p.Price,                           
    p.ProductYear                           
}).ToList();      

return prods;
Run Code Online (Sandbox Code Playgroud)