Reputation: 6227
I'm querying a repository for a list of Customer objects, but I've came across an error when calling ToObservableCollection()
on the returned list.
The specific error is when I call the QueryDataFromPersistence()
is:
'System.Collections.Generic.List<MongoDBApp.Models.CustomerModel>' does not contain a definition for 'ToObservableCollection' and no extension method 'ToObservableCollection' accepting a first argument of type 'System.Collections.Generic.List<MongoDBApp.Models.CustomerModel>' could be found
From Googling the error, it stated that this is an error, due to there being a mismatch in the assigned list and the returned list. But from checking both my CustomerRepositury and the MainViewModel, both lists are the same.
I also checked that both the Customer model and the MainViewModel, implement INotifyPropertyChanged which they do.
Does anyone know how to debug this further?
In the MainModel, the list is defined as follows and the QueryDataFromPersistence()
is called:
public ObservableCollection<CustomerModel> Customers
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
In the DataService class, the GetAllCustomers()
is called:
ICustomerRepository repository;
public List<CustomerModel> GetAllCustomers()
{
return repository.GetCustomers();
}
The after, in the DataRepository class, GetCustomer()
is called:
private static List<CustomerModel> customers = new List<CustomerModel>();
public List<CustomerModel> GetCustomers()
{
if (customers == null)
LoadCustomers();
return customers;
}
private void LoadCustomers()
{
var client = new MongoClient(connectionString);
var database = client.GetDatabase("orders");
//Get a handle on the customers collection:
var collection = database.GetCollection<CustomerModel>("customers");
try
{
customers = collection.Find(new BsonDocument()).ToListAsync().GetAwaiter().GetResult();
}
catch (MongoException ex)
{
//Log exception here:
MessageBox.Show("A connection error occurred: " + ex.Message, "Connection Exception", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
And the Customer
Model class:
public class CustomerModel : INotifyPropertyChanged
{
private ObjectId id;
private string firstName;
private string lastName;
private string email;
[BsonElement]
ObservableCollection<CustomerModel> customers { get; set; }
/// <summary>
/// This attribute is used to map the Id property to the ObjectId in the collection
/// </summary>
[BsonId]
public ObjectId Id { get; set; }
[BsonElement("firstName")]
public string FirstName
{
get
{
return firstName;
}
set
{
firstName = value;
RaisePropertyChanged("FirstName");
}
}
[BsonElement("lastName")]
public string LastName
{
get
{
return lastName;
}
set
{
lastName = value;
RaisePropertyChanged("LastName");
}
}
[BsonElement("email")]
public string Email
{
get
{
return email;
}
set
{
email = value;
RaisePropertyChanged("Email");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Upvotes: 0
Views: 2096
Reputation: 3845
You're getting the error because ToObservableCollection
does not currently exist as an extension method of IEnumerable<T>
.
You can either return an ObservableCollection
using the following syntax:
private void QueryDataFromPersistence()
{
Customers = new ObservableCollection<CustomerModel>(_customerDataService.GetAllCustomers());
}
Or even better you can write an extension method to encapsulate this:
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableResult)
{
return new ObservableCollection<T>(enumerableResult);
}
Then you can call it like you were originally.
Upvotes: 3
Reputation: 13367
Seems like:
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
Should be
private void QueryDataFromPersistence()
{
List<CustomerModel> listc = _customerDataService.GetAllCustomers();
Customers = new ObservableCollection(listc);
}
Upvotes: 3