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

2 posts, 0 answers
  1. Hung
    Hung avatar
    1 posts
    Member since:
    Apr 2015

    Posted 14 Apr 2015 Link to this post

    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?

  2. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2196 posts

    Posted 16 Apr 2015 Link to this post

    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.

     
Back to Top