无法在LINQto实体查询中构造实体有一个名为Products的实体类型,它是由实体框架生成的。我写了这个查询public IQueryable<Product> GetProducts(int categoryID){
return from p in db.Products
where p.CategoryID== categoryID select new Product { Name = p.Name};}下面的代码引发以下错误:“实体或复杂类型的Shop.Product不能在LINQtoEntites查询中构建”var products = productRepository.GetProducts(1).Tolist();但当我用select p而不是select new Product { Name = p.Name};它的工作原理是正确的。如何预置自定义选择部分?
4 回答
jeck猫
TA贡献1909条经验 获得超7个赞
public List<Product> GetProducts(int categoryID){ var query = from p in db.Products where p.CategoryID == categoryID select new { Name = p.Name }; var products = query.ToList().Select(r => new Product { Name = r.Name; }).ToList(); return products;}
public List<Product> GetProducts(int categoryID){ var query = from p in db.Products where p.CategoryID == categoryID select p; var products = query.ToList(); return products;}
- 4 回答
- 0 关注
- 566 浏览
添加回答
举报
0/150
提交
取消