简体   繁体   中英

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

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;

}

I want to get all fields in select new {...} , I wirte the select like that but all fields are not in Product table.....

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

how can I write the statement? please.....

Instead of select new , use select new Product . It will return a list of type Product:

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

return prods;

You're selecting an anonymous type in this line:

select new { ... }

Try this line:

select p

If you're trying to return a List<Product> , you won't be able to put ProductCategory information in the returned list unless Product has a navigational property that links to ProductCategory .

When you declare List as a type you should consider adding the correct templates

for example

public static List<Product> GetProductsByCategoryID(...)

You are creating an anonymous type, and you can't return an anonymous type from a method (there actually is a way but it is complicated compared to just making a new class and not worth it), and it doesn't match the signature of List either. So you'll have to create a new class that contains a definition for all the properties you want, for example ProductDetails, and return a List.

我遇到了同样的问题..你应该将List<Product> prods改为var prods

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM