Adding a collection of objects to another collection of objects without iteration

I have a collection of objcol1 objects (an example is a collection of cities in the state) and another collection of objcol2 objects (an example is a collection of cities in a country). Now I am requesting objcol1 and I want to add it to objcol2. I can do this by iterating through objcol1 and adding one to objcol2, but can I directly add objcol1 to objcol2 as objcol2.add (objcol1);

Can someone tell me if this is possible without iteration? If yes, please explain the process to me.

+5
source share
4 answers

You can use Enumerable.Concatthe extension method:

objcol1 = objcol1.Concat(objcol2)

, - , , .

. , City , Select .

+13

AddRange . . .

var a = new List<string> { "1", "2" };
var b = new List<string> { "3", "4" };
a.AddRange(b);

// a would contain "1", "2", "3" and "4"
+7

, . , "", linq Concat , .

var collection1 = new List<int> { 1, 2, 3 };
var collection2 = new [] { 4, 5, 6};

var concatenated = collection1.Concat(collection2);

, , , .

+2

var:

collection1.AddRange(collection2);
+1

All Articles