views:

163

answers:

1

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?

A: 

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.

SLaks
please read about my no go statement and then edit your answer, thanks.
msfanboy
@msfanboy: It is, by definition, completely impossible to do this in any other way. Please read about casting and classes and then edit your question, thanks.
SLaks
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.
SLaks
@SLaks I would mark your last comment as answer if you would put it in an answer... :)seems Linq + IEnumerable + ObservableCollection + aggregating business objects are a bad choice...
msfanboy
@msfanboy: Here you go. Note that there is nothing wrong with writing `new ObservableCollection`. It's not a bad choice, and it can work perfectly fine. (As long as you understand what the change events come from)
SLaks
of course there is nothing wrong with "new ObservableCollection" alone, never said that I used another context.
msfanboy