C#使用linq连接两个Collection <string>并获取Collection <string>结果

Lan*_*ing 5 c# linq linq-to-objects

我正在尝试这样做:

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

var result = collection1.Concat(collection2);
Run Code Online (Sandbox Code Playgroud)

但结果变量是类型Enumerable [System.String],而我想要一个Collection [System.String]

我试过铸造:

var all = (Collection<string>) collection1.Concat(collection2);
Run Code Online (Sandbox Code Playgroud)

但没有快乐.

Jam*_*ran 13

var result = new Collection<string>(collection1.Concat(collection2).ToList());
Run Code Online (Sandbox Code Playgroud)

由于某种原因,System.Collections.ObjectModel.Collection需要IList参数给它的构造函数.(其他收藏品只需要一个IEnumerator)


Pau*_*ane 5

Enumerable.ToList()原样使用。List<>ICollection<>

例如:

IList list = a.Concat(b).ToList()
Run Code Online (Sandbox Code Playgroud)

如果您的意思System.ObjectModel.Collection<>是那么您必须将创建的列表传递到 的构造函数中Collection<>,我知道这并不理想。

var collection = new System.ObjectModel.Collection<string>(a.Concat(b).ToList());
Run Code Online (Sandbox Code Playgroud)