gpt4 book ai didi

c# - 如何解决 “does not contain a definition for ' ToObservableCollection'”错误?

转载 作者:行者123 更新时间:2023-12-03 10:53:21 25 4
gpt4 key购买 nike

我正在查询存储库中的Customer对象列表,但是在返回的列表上调用ToObservableCollection()时遇到错误。

具体的错误是当我调用QueryDataFromPersistence()时:

'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 

从Googling错误中可以看出,这是一个错误,因为分配的列表和返回的列表不匹配。但是从检查我的CustomerRepositury和MainViewModel来看,两个列表是相同的。

我还检查了Customer模型和MainViewModel是否都实现了INotifyPropertyChanged。

有人知道如何进一步调试吗?

在MainModel中,列表定义如下,并调用 QueryDataFromPersistence():
        public ObservableCollection<CustomerModel> Customers

private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();

}

在DataService类中, GetAllCustomers()称为:
ICustomerRepository repository;

public List<CustomerModel> GetAllCustomers()
{
return repository.GetCustomers();
}

之后,在DataRepository类中, GetCustomer()称为:
       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);
}

}

以及 Customer Model类:
    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));
}
}
}

最佳答案

您收到错误消息是因为ToObservableCollection目前不存在作为IEnumerable<T>的扩展方法。

您可以使用以下语法返回ObservableCollection:

private void QueryDataFromPersistence()
{
Customers = new ObservableCollection<CustomerModel>(_customerDataService.GetAllCustomers());
}

甚至更好的是,您可以编写扩展方法来封装此代码:
public static ObservableCollection<T> ToObservableCollection<T>(this IEnumerable<T> enumerableResult)
{
return new ObservableCollection<T>(enumerableResult);
}

然后,您可以像原来一样调用它。

关于c# - 如何解决 “does not contain a definition for ' ToObservableCollection'”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768164/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com