我有一个对象objcol1(一个州的城市示例集合)和另一个对象集合objcol2(一个国家中的城市示例集合).现在,我正在查询objcol1,我想将其添加到objcol2中.我可以通过遍历objcol1并将其一一添加到objcol2来做到这一点,但是我可以像这样直接将objcol1添加到objcol2吗? objcol2.add(objcol1);
I have a collection of objects objcol1(example Collection of cities in a state) and another object collection objcol2(example collection of cities in a country). Now I am querying for objcol1 and I want to add it to objcol2. I can do this by iterating through objcol1 and adding one by one to objcol2 but can I directly add objcol1 to objcol2 like objcol2.add(objcol1);
有人可以告诉我是否可以不进行迭代?如果是,请向我解释该过程
Can anyone tell me whether it is possible without iterating? If yes please explain me the process
推荐答案您可以使用 Enumerable.Concat 扩展方法:
You could use the Enumerable.Concat extension method:
objcol1 = objcol1.Concat(objcol2)我敢肯定,它实际上是在迭代的地方进行的,但是您无需编写代码即可完成操作.
I'm sure under the covers somewhere it actually iterates, but you won't need to write the code to do it.
注意:仅当您的City对象相同时,此方法才有效,或者您可以使用Select映射对象.
NOTE: This will only work if your City objects are the same, alternatively you could use Select to map the objects.
更多推荐
无需迭代即可将对象集合添加到另一个对象集合
发布评论