开发者

Wrap an IEnumerable<T> with an ObservableCollection<T>

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)
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜