开发者

Cast IEnumerable<T> to ObservableCollection<T> without constructor injection the IEnumerable<T>

开发者 https://www.devze.com 2023-01-07 15:11 出处:网络
How can I do that? thats a no go: ObservableCollection obsCol = newObservableCollection(myIEnumerable);

How can I do that?

thats a no go:

ObservableCollection obsCol = new ObservableCollection(myIEnumerable);

scenario:

var query = from c in customers
                    select new Customer()
                    {
                       Products = from p in products
                                  where p.Id = c.Id
                               开发者_如何学运维   select p
};

Products is a ObservableCollection so it can not take the IEnumerable result from the select above...

How to cast?


Like this:

ObservableCollection obsCol = new ObservableCollection(myIEnumerable.ToList());

Note that the ObservableCollection instance will not reflect changes to the customers list.

EDIT: The Select extension method returns an instance of a compiler-generated iterator class. Since this class does not inherit ObservableCollection, it is impossible to cast it to one.

0

精彩评论

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