C # concat two Collection <string> using linq and getting Collection <string> result

I am 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 an Enumerable [System.String] type, whereas I need a [System.String] collection

I tried casting:

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

But the joys.

+5
source share
2 answers
var result = new Collection<string>(collection1.Concat(collection2).ToList());

For some reason, System.Collections.ObjectModel.Collection requires an IList constructor for it. (The rest of the collections only need IEnumerator)

+12
source

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

eg:.

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

System.ObjectModel.Collection<>, Collection<>, , .

var collection = new System.ObjectModel.Collection<string>(a.Concat(b).ToList());
+4

All Articles