From my 开发者_如何学GoRepository I get always an IEnumerable<T>
.
In the Ctor or a method of my ViewModel I want to wrap my entities into an ObservableCollection<T>
like this:
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);
}
What would you do?
Assuming you have some sort of conversion from Customer to CustomerViewModel:
customerViewModels = new ObservableCollection<CustomerViewModel>
(customers.Select(c => ConvertToViewModel(c)));
How do you convert a Customer
to a CustomerViewModel
? Maybe you just need something like this:
customerViewModels = new ObservableCollection<CustomerViewModel>(
from c in customers
select new CustomerViewModel(c)
);
精彩评论