views:

79

answers:

2

I'm trying to do this:

var collection1 = new Collection<string> {"one", "two"};
var collection2 = new Collection<string> {"three", "four"};

var result = collection1.Concat(collection2);

But the result variable is type Enumerable[System.String] , whereas I want a Collection[System.String]

I've tried casting:

var all = (Collection<string>) collection1.Concat(collection2);

But no joy.

+2  A: 

Use Enumerable.ToList(), as List<> is an ICollection<>.

E.g.:

IList list = a.Concat(b).ToList()

If you meant System.ObjectModel.Collection<> then you will have to pass the created list into the constructor of Collection<>, not ideal I know.

var collection = new System.ObjectModel.Collection(a.Concat(b).ToList()); var collection = new System.ObjectModel.Collection(a.Concat(b).ToArray());

Paul Ruane
+2  A: 
var result = new Collection<string>(collection1.Concat(collection2).ToList());

For some reason System.Collections.ObjectModel.Collection requires an IList parameter to it's constructor. (The other collections only need an IEnumerator)

James Curran