无需迭代即可将对象集合添加到另一个对象集合

编程入门 行业动态 更新时间:2024-10-27 07:17:05
本文介绍了无需迭代即可将对象集合添加到另一个对象集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个对象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.

更多推荐

无需迭代即可将对象集合添加到另一个对象集合

本文发布于:2023-11-22 09:54:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:将对   对象   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!