C# 如何解决“问题”;不包含';ToObservableCollection'&引用;错误?
我正在查询存储库中的客户对象列表,但在对返回的列表调用C# 如何解决“问题”;不包含';ToObservableCollection'&引用;错误?,c#,list,mvvm,observablecollection,assign,C#,List,Mvvm,Observablecollection,Assign,我正在查询存储库中的客户对象列表,但在对返回的列表调用ToObservableCollection()时遇到错误 具体错误是在调用QueryDataFromPersistence()is时: 'System.Collections.Generic.List<MongoDBApp.Models.CustomerModel>' does not contain a definition for 'ToObservableCollection' and no extension metho
ToObservableCollection()
时遇到错误
具体错误是在调用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
在DataService类中,调用GetAllCustomers()
:
public ObservableCollection<CustomerModel> Customers
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
ICustomerRepository repository;
public List<CustomerModel> GetAllCustomers()
{
return repository.GetCustomers();
}
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);
}
}
以及客户
车型类别:
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));
}
}
}
公共类CustomerModel:INotifyPropertyChanged
{
私有ObjectId;
私有字符串名;
私有字符串lastName;
私人字符串电子邮件;
[b单一元素]
ObservableCollection客户{get;set;}
///
///此属性用于将Id属性映射到集合中的ObjectId
///
[BsonId]
公共对象Id{get;set;}
[b单一元素(“名字”)]
公共字符串名
{
得到
{
返回名字;
}
设置
{
firstName=值;
RaisePropertyChanged(“名字”);
}
}
[b单一元素(“姓氏”)]
公共字符串姓氏
{
得到
{
返回姓氏;
}
设置
{
lastName=值;
RaisePropertyChanged(“姓氏”);
}
}
[b单一元素(“电子邮件”)]
公共字符串电子邮件
{
得到
{
回复邮件;
}
设置
{
电子邮件=价值;
RaisePropertyChanged(“电子邮件”);
}
}
公共事件属性更改事件处理程序属性更改;
私有void RaisePropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
{
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
}
}
看起来像:
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
应该是
private void QueryDataFromPersistence()
{
List<CustomerModel> listc = _customerDataService.GetAllCustomers();
Customers = new ObservableCollection(listc);
}
private void querydata fromPersistence()
{
List listc=_customerDataService.GetAllCustomers();
客户=新的可观察收集(listc);
}
看起来像:
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
应该是
private void QueryDataFromPersistence()
{
List<CustomerModel> listc = _customerDataService.GetAllCustomers();
Customers = new ObservableCollection(listc);
}
private void querydata fromPersistence()
{
List listc=_customerDataService.GetAllCustomers();
客户=新的可观察收集(listc);
}
出现错误是因为ToObservableCollection
当前不作为IEnumerable
的扩展方法存在
您可以使用以下语法返回一个ObservableCollection
:
private void QueryDataFromPersistence()
{
Customers = new ObservableCollection<CustomerModel>(_customerDataService.GetAllCustomers());
}
private void querydata fromPersistence()
{
Customers=新的observeCollection(_customerDataService.GetAllCustomers());
}
或者更好的是,您可以编写一个扩展方法来封装:
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableResult)
{
return new ObservableCollection<T>(enumerableResult);
}
公共静态ObservableCollection ToObservableCollection(此IEnumerable enumerableResult)
{
返回新的ObservableCollection(EnumerablerResult);
}
然后,您可以像最初一样调用它。您会收到错误,因为
ToObservableCollection
目前不作为IEnumerable
的扩展方法存在
您可以使用以下语法返回一个ObservableCollection
:
private void QueryDataFromPersistence()
{
Customers = new ObservableCollection<CustomerModel>(_customerDataService.GetAllCustomers());
}
private void querydata fromPersistence()
{
Customers=新的observeCollection(_customerDataService.GetAllCustomers());
}
或者更好的是,您可以编写一个扩展方法来封装:
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableResult)
{
return new ObservableCollection<T>(enumerableResult);
}
公共静态ObservableCollection ToObservableCollection(此IEnumerable enumerableResult)
{
返回新的ObservableCollection(EnumerablerResult);
}
然后你可以像最初一样调用它。好的,那么基本上我需要将返回的列表转化为一个可观察的客户集合?这是正确的吗?如果不是,你能进一步解释解决方案吗?谢谢Lynn@BrianJ是的-这就是为什么您以前尝试使用ToObservableCollection()的原因,对吗?为了得到一个可观察的集合……我接受了另一个答案,因为他提供了一个扩展方法的使用。你的回答同样有效,这就是我投票的原因:)@BrianJ别担心。。。很高兴你找到了解决方案。好的,那么基本上我需要将返回的列表转化为一个可观察的客户集合?这是正确的吗?如果不是,你能进一步解释解决方案吗?谢谢Lynn@BrianJ是的-这就是为什么您以前尝试使用ToObservableCollection()的原因,对吗?为了得到一个可观察的集合……我接受了另一个答案,因为他提供了一个扩展方法的使用。你的回答同样有效,这就是我投票的原因:)@BrianJ别担心。。。很高兴你找到了解决办法。