开发者

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

开发者 https://www.devze.com 2023-01-26 19:27 出处:网络
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:

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

精彩评论

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