如何解决“;不包含';ToObservableCollection'";错误

本文关键字:何解决 ToObservableCollection quot 错误 包含 解决 | 更新日期: 2023-09-27 18:22:14

我正在存储库中查询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 

通过谷歌搜索该错误,它表明这是一个错误,因为分配的列表和返回的列表不匹配。但通过检查我的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模型类:

    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'";错误

由于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);
}

然后你可以像原来那样称呼它。

看起来像:

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

应该是

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