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.
精彩评论