0

I'm new to WPF and I'm struggling. I need to populate a combobox with data from a database. I'm attempting to use LINQ to retrieve the data into an ObservableCollection which I am binding to through the XAML. Simple probably for everyone except me. I'm getting an error "The entity or complex type '' cannot be constructed in a LINQ to Entities query." when I attempt to populate the collection. What am I doing wrong? I've looked at several different questions, like this one and this one, but without any luck. This seems so simple. What am I doing wrong?

Here is what my ViewModel looks like:

public class MainViewModel : ObservableObject
{
    private ObservableCollection<Customer> _customers;
    public ObservableCollection<Customer> Customers
    {
        get { return _customers; }
        set
        {
            if ( value != _customers )
            {
                _customers = value;
                OnPropertyChanged( "Customers" );
            }
        }
    }

    public MainViewModel()
    {
        // populate the customers combo
        using ( var db = new DataContext() )
        {
            var customer = ( from c in db.Customers
                           where c.Active == true
                           select new Customer
                           {
                               CustomerID = c.CustomerID,
                               Name = c.Name
                           } );
            // Error Here
            Customers = new ObservableCollection<Customer>( customer.ToList() );
            // Tried this too
            //var Query = db.Customers.Select( c => new Customer() { Name=c.Name, CustomerID=c.CustomerID } ).ToList<Customer>();
            //Customers = new ObservableCollection<Customer>(Query);
        }
    } 
}

EDIT: Here is a copy of the Customer class:

namespace EF_HVAC_Estimator.Models
{
    [Table( "Customers" )]
    public class Customer : ModelBase
    {
        [Key]
        public int CustomerID { get; set; }

        public int EmployeeID { get; set; }

        [StringLength( 100 )]
        public string Name { get; set; }

        [StringLength( 100 )]
        public string Address { get; set; }

        [StringLength( 100 )]
        public string City { get; set; }

        public int StateID { get; set; }

        [StringLength( 10 )]
        public string Zip { get; set; }

        [StringLength( 15 )]
        public string Phone { get; set; }

        [StringLength( 15 )]
        public string Fax { get; set; }

        [StringLength( 50 )]
        public string EmailAddress { get; set; }

        public bool Active { get; set; }

        public List<Estimate> Estimates { get; set; }
    }
}
Community
  • 1
  • 1
mack
  • 2,298
  • 8
  • 33
  • 56
  • Provide some details on the class Customer? – Praveen Paulose Apr 03 '15 at 16:37
  • Also check this http://stackoverflow.com/questions/5325797/the-entity-cannot-be-constructed-in-a-linq-to-entities-query – Praveen Paulose Apr 03 '15 at 16:39
  • An observable collection is not a requirement to showing/binding a list. Only use an observable collection if you need to be notified if a list changes, additions/deletions. Since it doesn't appear that you need the overhead of the observable collection, just use a List instead. – ΩmegaMan Apr 03 '15 at 16:52
  • Loop thru db.Customers and add to Customers in the loop – paparazzo Apr 03 '15 at 16:54
  • Thanks @PraveenPaulose, I added the Customers class. – mack Apr 03 '15 at 17:10

1 Answers1

1

It looks like you're getting a collection of the Entity Customer and trying to cast it into Customer. Perhaps it is getting confused.

Linq uses deferred execution and is trying to pass the query down to the database. It is the ToList() method that is triggering the execution. If the 2 customer classes (the query and the type in the observable collection) are really the same entity, just fill the collection with the result.

Note that unless you're dynamically adding/removing items from the collection, there may be no reason to use an ObservableCollection. A List<Customer> will suffice. Are the Customer classes intended to be the same, or is the ObservableCollection a different Customer class? Specifying the namespace may help, if that is the case.

using(var db = new DataContext())
{
  Customers = db.Customers
                .Where(c => c.Active == true).ToList();
}

[EDIT] Alright then... simply iterate over the list to populate... should be the same result. I think the problem was lying in the cast you were doing during the Linq select.

using(var db = new DataContext())
{
    var custs = db.Customers
                  .Where(c => c.Active == true).ToList();

    if(Customers == null)
        Customers = new ObservableCollection<Customer>();
    foreach (var cust in custs)
    {
        Customers.Add(cust);
    }
}
Cameron
  • 2,464
  • 21
  • 35
Mark W
  • 1,034
  • 7
  • 15
  • Thanks @Mark W, changing the code to use a list works, but there will be some adding/removing going on so in the end I will need to use an ObservableCollection. – mack Apr 06 '15 at 16:40