作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在查询存储库中的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
QueryDataFromPersistence()
:
public ObservableCollection<CustomerModel> Customers
private void QueryDataFromPersistence()
{
Customers = _customerDataService.GetAllCustomers().ToObservableCollection();
}
GetAllCustomers()
称为:
ICustomerRepository repository;
public List<CustomerModel> GetAllCustomers()
{
return repository.GetCustomers();
}
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/
我知道我可以创建一个 List来自 IEnumerable通过做 myEnumerableCollection.ToList() ,但我怎么能为 ObservableCollection 实现同样的事
我正在查询存储库中的Customer对象列表,但是在返回的列表上调用ToObservableCollection()时遇到错误。 具体的错误是当我调用QueryDataFromPersistence(
我是一名优秀的程序员,十分优秀!