简体   繁体   中英

The entity or complex type ' ' cannot be constructed in a LINQ to Entities query

I keep getting an error and I can't figure out the source of it. I'm trying to do a simple image upload connected to an item and when trying to pull the list I get the following error:

The entity or complex type ' ' cannot be constructed in a LINQ to Entities query

public ActionResult Index()
{
    var content = db.Items.Select(s => new
    {
        s.Id,
        s.Image,
        s.Price,
        s.Quantity,
    });

    List<Item> contentModel = content.Select(item => new Item()
    {
        Id = item.Id,
        Image = item.Image,
        Price = item.Price,
        Quantity = item.Quantity,
    }).ToList();

    return View(contentModel);
}

如果您的数据源是 SQL 数据库,则必须在使用不可翻译为 SQL 的表达式调用Select之前调用ToList

content.ToList().Select(...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM