The entity cannot be constructed in a LINQ to Entities query

前端 未结 14 2354
失恋的感觉
失恋的感觉 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:41

    Another simple way :)

    public IQueryable GetProducts(int categoryID)
    {
        var productList = db.Products
            .Where(p => p.CategoryID == categoryID)
            .Select(item => 
                new Product
                {
                    Name = item.Name
                })
            .ToList()
            .AsQueryable(); // actually it's not useful after "ToList()" :D
    
        return productList;
    }
    

提交回复
热议问题