LINQ to Entities中只支持无参数构造函数和初始化器? [英] Only parameterless constructors and initializers are supported in LINQ to Entities?
本文介绍了LINQ to Entities中只支持无参数构造函数和初始化器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
public class ProductViewModel:IViewModel {
public ProductViewModel(){
Categories = new List< CategoryViewModel>();
}
#region DTO帮助者
public ProductViewModel(Product p){
this.ID = p.ID;
this.Name = p.Name;
this.Price = p.Price;
Categories = new List< CategoryViewModel>();
}
#endregion
public int ID {get;组; }
public string Name {get;组; }
public decimal价格{get;组; }
public IEnumerable< CategoryViewModel>分类{get;组;
}
我以前用过LINQ2SQL这个代码,使用实体框架它不会:
var products =(from p in db.GetAll()
select new ProductViewModel(p));
我收到此错误:
LINQ to Entities中只支持无参数构造函数和初始值设置
任何人有帮助解释/解决这个问题?
解决方案
var products = (from p in db.GetAll()
选择新的ProductViewModel {
ID = p.Id,
....
});
I have a ProductViewModel that converts a DTO (Product) at runtime.
public class ProductViewModel : IViewModel {
public ProductViewModel() {
Categories = new List<CategoryViewModel>();
}
#region DTO Helpers
public ProductViewModel(Product p) {
this.ID = p.ID;
this.Name = p.Name;
this.Price = p.Price;
Categories = new List<CategoryViewModel>();
}
#endregion
public int ID { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public IEnumerable<CategoryViewModel> Categories { get; set; }
}
I've used this code with LINQ2SQL before and it's worked, but now with entity framework it doesn't:
var products = (from p in db.GetAll()
select new ProductViewModel(p));
I get this error:
Only parameterless constructors and initializers are supported in LINQ to Entities
Can anybody help explain/fix this?
解决方案
var products = (from p in db.GetAll()
select new ProductViewModel{
ID = p.Id,
....
});
这篇关于LINQ to Entities中只支持无参数构造函数和初始化器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文