71

The fact that it is a LINQ result might perhaps not be relevant for the question, but I'm mentioning it anyway - since this is the context which has resulted in this question.

I run a LINQ query. The result is an;

IEnumerable<MyClass> 

I want to put the result into an ObservableCollection;

ObservableCollection<MyClass> 

How do I do this cast? (without running through the IEnumerable and copying elements to the ObservableCollection). I notice LINQ has got a few To..() functions, but it doesn't seem to help me for this cast..?

stiank81
  • 25,418
  • 43
  • 131
  • 202
  • Does this answer your question? [How to convert IEnumerable to ObservableCollection?](https://stackoverflow.com/questions/3559821/how-to-convert-ienumerable-to-observablecollection) – StayOnTarget Feb 23 '21 at 17:12

6 Answers6

116

Just use:

ObservableCollection<Foo> x = new ObservableCollection<Foo>(enumerable);

That will do the required copying. There's no way of observing changes to the live query - although the idea of an ObservableQuery<T> is an interesting (though challenging) one.

If you want an extension method to do this, it's simple:

public static ObservableCollection<T> ToObservableCollection<T>
    (this IEnumerable<T> source)
{
    if (source == null)
    {
        throw new ArgumentNullException("source");
    }
    return new ObservableCollection<T>(source);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 4
    Anybody now how to achieve this in Silverlight 3.0, where only the default new ObservableCollection() constructor is available? – Bernard Vander Beken Dec 02 '09 at 10:43
  • 1
    Unfortunately that's not much use if you want to copy the results into an ObservableCollection that is already wrapped by a ReadOnlyObservableCollection as the ReadOnlyObservableCollection cannot be rebound to a new collection. In this case it seems you are stuck using a foreach loop and copying the elements one at a time. – Neutrino Jan 21 '14 at 17:12
  • @Neutrino: I'd argue that's a different problem than the one presented in the question then. It's a valid problem, I just don't think it's the one the OP had. – Jon Skeet Jan 21 '14 at 17:15
  • I agree. I didn't mean to suggest that your post didn't answer the original question, just hoping someone might have a thought on the ReadOnlyObservableCollection issue. – Neutrino Jan 22 '14 at 09:04
16
var linqResults = foos.Where(f => f.Name == "Widget");

var observable = new ObservableCollection<Foo>(linqResults);
Dave Markle
  • 95,573
  • 20
  • 147
  • 170
11

You can use an ObservableCollection constructor for this:

ObservableCollection<MyClass> obsCol = 
        new ObservableCollection<MyClass>(myIEnumerable);
bruno conde
  • 47,767
  • 15
  • 98
  • 117
0

IEnumerable is only the interface.

You would need to copy the content from the IEnumerable into the ObservableCollection. You can do this by passing your IEnumerable into the constructor of the ObersvableCollection when you create a new one

Heiko Hatzfeld
  • 3,197
  • 18
  • 15
0

You need my ObservableComputations library maybe. That is .NET API for computations over INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects. Results of the computations are INotifyPropertyChanged and INotifyColectionChanged (ObservableCollection) objects.

Igor Buchelnikov
  • 505
  • 4
  • 14
-1

I wrote this library a few years ago.

https://github.com/wasabii/OLinq

It doesn't do exactly what you probably want, it does more. It's a Linq query provider which parses the expression tree, attaches to the referenced collections, and exposes another collection which emits events upon changes.

It's missing a few Linq operators. But the code base is not hard to extend.

Jerome Haltom
  • 1,670
  • 2
  • 17
  • 23
  • I've also been maintaining NuGet packages for it. https://www.nuget.org/packages/OLinq/ – Jerome Haltom Mar 01 '15 at 18:00
  • I wanted to use your package, but the example provided in the readme isn't working, the call to `ToObservableView` after the `Where` is unknown – Florian Koch Apr 18 '16 at 12:53
  • 1
    Not sure of the README. Looks like somebody mucked it up in the past. .Where returns an IQueryable. But the actual object is a ObservableQuery. I have a .AsObservableQuery() method to do the cast. >collection.AsObservableQuery().Where(i => i > 2).AsObservableQuery().ToObservableView() Not unlike .AsEnumerable and AsParallelQuery and all those others. – Jerome Haltom Apr 19 '16 at 17:02