如何解决 "does not contain a definition for 'ToObservableCollection'" 错误?

How to resolve a "does not contain a definition for 'ToObservableCollection'" error?

我正在为客户对象列表查询存储库,但在返回列表上调用 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 class 中,GetAllCustomers() 被调用:

ICustomerRepository repository;

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

之后,在DataRepository中class,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模型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));
            }
        }
    }

好像是:

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

应该是

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

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

您可以使用以下语法 return 或 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);
}

然后你就可以像原来一样调用它了。