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; }
}
}