In LINQ for Entities, only parameterless constructors and initializers are supported?

I have a ProductViewModel that converts 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 used this code with LINQ2SQL before, and it worked, but now with the entity infrastructure this is not the case:

        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 someone help explain / fix this?

0
source share
2 answers
var products = (from p in db.GetAll()
               select new ProductViewModel{
                   ID = p.Id,
                   ....
               });
0
source

To get all the data from one object, use

Context.Set<your entity>().AsQueryable();
0
source

All Articles