The entity cannot be constructed in a LINQ to Entities query

前端 未结 14 2070
失恋的感觉
失恋的感觉 2020-11-21 06:04

There is an entity type called Product that is generated by entity framework. I have written this query

public IQueryable GetProdu         


        
14条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-21 06:58

    There is another way that I found works, you have to build a class that derives from your Product class and use that. For instance:

    public class PseudoProduct : Product { }
    
    public IQueryable GetProducts(int categoryID)
    {
        return from p in db.Products
               where p.CategoryID== categoryID
               select new PseudoProduct() { Name = p.Name};
    }
    

    Not sure if this is "allowed", but it works.

提交回复
热议问题