This is a migrated thread and some comments may be shown as answers.

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

1 Answer 240 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hung
Top achievements
Rank 1
Hung asked on 14 Apr 2015, 10:31 AM

I'm starting to build a Telerik UI for ASP.NET MVC project, just like the Grid Demo on http://demos.telerik.com/aspnet-mvc/

And i got this error when i debug project.

What problem?

1 Answer, 1 is accepted

Sort by
0
Vladimir Iliev
Telerik team
answered on 16 Apr 2015, 08:35 AM
Hello Hung,

From the provided information it seems that you are trying to project the entities returned by the "northwind.Customers" to existing entity type which is not supported. In current case I would suggest to create "CustomersViewModel" class and update the "GetCustomers" method as follows:

private static IEnumerable<CustomerViewModel> GetCustomers()
{
    var northwind = new SampleEntities();
    return northwind.Customers.Select(customer => new CustomerViewModel
    {
        CustomerID = customer.CustomerID,
        CompanyName = customer.CompanyName,
        ContactName = customer.ContactName,
        ContactTitle = customer.ContactTitle,
        Address = customer.Address,
        City = customer.City,
        Region = customer.Region,
        PostalCode = customer.PostalCode,
        Country = customer.Country,
        Phone = customer.Phone,
        Fax = customer.Fax,
        Bool = customer.Bool
    });
}

public class CustomerViewModel
{
    public string CustomerID { get; set; }
    public string CompanyName { get; set; }
    public string ContactName { get; set; }
    public string ContactTitle { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
    public string Country { get; set; }
    public string Phone { get; set; }
    public string Fax { get; set; }
    public Nullable<bool> Bool { get; set; }
}
Regards,
Vladimir Iliev
Telerik
 

See What's Next in App Development. Register for TelerikNEXT.

 
Tags
Grid
Asked by
Hung
Top achievements
Rank 1
Answers by
Vladimir Iliev
Telerik team
Share this question
or