views:

503

answers:

2

Hi folks,

I have two ObservableCollection lists, that i want to unite. My naive approach was to use the Union - Method:

ObservableCollection<Point> unitedPoints = observableCollection1.Union(observableCollection2);

ObservableCollection1/2 are of type ObservableCollection too. But the Compiler throws the following error for this line:

The Type "System.Collections.Generic.IEnumerable" can't be converted implicit to "System.Collections.ObjectModel.ObservableCollection". An explicite conversion already exists. (Maybe a conversion is missing)

(wording may not be exact, as I translated it from german).

Anyone knows, how to merge both ObservableCollections and get an ObservableCollection as result?

Thanks in advance, Frank

Edith says: I just realized that it is important to mention that I develop a Silverlight-3-Application, because the class "ObservableCollection" differ in SL3 and .NET3.0 scenarios.

+4  A: 

Do you want to merge the existing contents, but then basically have independent lists? If so, that's relatively easy:

ObservableCollection<Point> unitedPoints = new ObservableCollection<Point>
    (observableCollection1.Union(observableCollection2).ToList());

However, if you want one observable collection which is effectively a "view" on others, I'm not sure the best way to do that...

Jon Skeet
I tried this already, but the Constructor of the ObservableCollection had no overloads that accept a parameter ... and then I realized that ObservableCollection in Silverlight is different to OberservableCollection in .NET 3.0. WHAT? I thought I can use the same classes from Desktop apps in Silverlight? HOW should this work, when even such basic classes differ in both scenarios? HOW can they even think about giving DIFFERENT classes the SAME name? That's absolutely weird. What else works *a little bit* different then ...
Aaginor
Are you sure they're actually different classes? Or is it just that the Silverlight version is missing some methods/constructors?
Jon Skeet
@aaginor: Silverlight implementations of many things that they have in common with the standard .NET libraries are often considerably simplified. Its quite irritating having to check their docs all over again when using something we're already familiar with. Its especially irritating that SL3 docs are still only in chm (or online) form :(
AnthonyWJones
@Jon: Imho they are different, even if one is missing only a single method/constructor/property. The point is (and this was the one I was VERY excited about), that it seemed that I could use all my .NET-3.0 classes (with exceptions, yes, but that are "logical" ones like no Windows.Forms) and use them in Silverlight. Now I always have to ask me "will they work the same way? Maybe there is a small difference - and my code is becoming bugged because of this difference? And maybe the difference is of such a kind, that the compiler doesn't throw an error ..."
Aaginor
@Anthony: Is there some kind of comparison list between .NET3.0 and SL3? Such a thing like a whitelist, so I could tell which classes to use without to fear something breaks, a greylist with the differences (like the ObservableCollection) and a blacklist (like the HashSet) ...
Aaginor
+5  A: 

The LINQ Union extension method returns an IEnumerable. You will need to enumerate and add each item to the result collection:-

var unitedPoints = new ObservableCollection<Point> ();
foreach (var p in observableCollection1.Union(observableCollection2))
   unitedPoints.Add(p);

If you'd like a ToObservableCollection then:-

public static class MyEnumerable
{
    public static ObservableCollection<T> ToObservableCollection(this IEnumerable<T> source)
    {
        var result = new ObservableCollection<T> ();
        foreach (var x in source)
           result.Add(p);
        return result;
    }
 }

Now your line is:-

var unitedPoints = observableCollection1.Union(observableCollection2).ToObservableCollection();
AnthonyWJones
nice answer - but did you mean foreach, not 'for'?
IanR
yap, it's foreach
Aaginor
ooops, yes foreach, fliting between javascript and C# will do that to you.
AnthonyWJones
uh .. javascript ... coming from a typesafe world with explicite declarations ... I felt dirty after dealing with Javascript for about an hour *g*
Aaginor
You can save yourself some code on the ToObservableCollection method :) return new ObservableCollection<TSource>(source);
JustEngland
@JustEngland: In Silverlight 4 you can but Silverlight 3 doesn't have that constructor.
AnthonyWJones