开发者

How to concatenate to collections in asp.net?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜