开发者

How to concatenate to collections in asp.net?

开发者 https://www.devze.com 2023-02-04 09:55 出处:网络
I use the following code. Collection<MyClass> MyCollection1 = new Collection<MyClass>(); MyCollection.Concat(GetSecondCollcetion());

I use the following code.

Collection<MyClass> MyCollection1 = new Collection<MyClass>(); MyCollection.Concat(GetSecondCollcetion());

No matter what function GetSecondCollcetion() returns(Obviously Object of Collection<MyClass>) the MyCollection1开发者_StackOverflow社区 Always Empty. Please Help


Try (if you don't explicitly need MyCollection to be a variable of Collection<MyClass>:

var MyCollection1 = new Collection<MyClass>().Concat(GetSecondCollcetion());

Alternatively

Collection<MyClass> MyCollection1 = new Collection<MyClass>(GetSecondCollection()); 

Or as @Cine says:

MyCollection.AddRange(GetSecondCollcetion());


The Linq method Concat doesn't change the first collection, it returns a new (third) collection with the result: the combination of the two input collections.

So use:

MyCollection1 = MyCollection1.Concat(GetSecondCollcetion()).ToList();

Or use AddRange or one of the other suggestions.

0

精彩评论

暂无评论...
验证码 换一张
取 消