从我的存储库中,我总是得到一个IEnumerable<T>
。
在Ctor或ViewModel的方法中,我想将我的实体包装成ObservableCollection<T>
,如下所示:
private ObservableCollection<CustomerViewModel> customerViewModels;
public BillingViewModel(IService service)
{
...
IEnumerable<Customer> customers = service.GetCustomers();
// that will not work because of a different type
customerViewModels = new ObservableCollection(customers);
}
你会怎么做?
最佳答案
假设您有某种从Customer到CustomerViewModel的转换:
customerViewModels = new ObservableCollection<CustomerViewModel>
(customers.Select(c => ConvertToViewModel(c)));
关于wpf - 将IEnumerable <T>与ObservableCollection <T>包装在一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4280900/