附加/连接两个 IEnumerable 序列
我有两组数据行。它们都是 IEnumerable。我想将这两个列表附加/连接到一个列表中。我确信这是可行的。我不想做for循环,注意到两个List上有一个Union方法和一个Join方法。有什么想法吗?
I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I'm sure this is doable. I don't want to do a for loop and noticed that there is a Union method and a Join method on the two Lists. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
假设您的对象属于同一类型,您可以使用
Union
或Concat
。请注意,与 SQLUNION
关键字一样,Union
操作将确保消除重复项,而Concat
(如UNION ALL< /code>) 只会将第二个列表添加到第一个列表的末尾。
或者
如果它们属于不同类型,那么您必须将它们
选择
为共同的类型。例如:其中
ConvertToT(TOne f)
和ConvertToT(TTwo s)
表示以某种方式转换TOne
实例的操作(并且>TTwo
,分别)到T
的实例中。Assuming your objects are of the same type, you can use either
Union
orConcat
. Note that, like the SQLUNION
keyword, theUnion
operation will ensure that duplicates are eliminated, whereasConcat
(likeUNION ALL
) will simply add the second list to the end of the first.or
If they are of different types, then you'll have to
Select
them into something common. For example:Where
ConvertToT(TOne f)
andConvertToT(TTwo s)
represent an operation that somehow converts an instance ofTOne
(andTTwo
, respectively) into an instance ofT
.我刚刚遇到了类似的情况,需要连接多个序列。
自然地在 Google/StackOverflow 上搜索现有解决方案,但是没有找到任何未评估可枚举的内容,例如转换为数组然后使用 Array.Copy() 等,所以我编写了一个扩展和静态名为 ConcatMultiple 的实用方法。
希望这可以帮助任何需要这样做的人。
I just encountered a similar situation where I need to concatenate multiple sequences.
Naturally searched for existing solutions on Google/StackOverflow, however did not find anything the did not evaluate the enumerable, e.g. convert to array then use
Array.Copy()
etc., so I wrote an extension and static utiltiy method calledConcatMultiple
.Hope this helps anyone that needs to do the same.
加入方法类似于 SQL 连接,其中列表根据条件交叉引用,它不是字符串连接或添加到列表。 Union 方法确实可以满足您的要求,就像Concat 方法,但都是 LAZY 评估,并且要求参数非空。它们返回 ConcatIterator 或 UnionIterator,并且 如果重复调用,这可能引起问题。急切的评估会导致不同的行为,如果这是您想要的,那么可以使用如下的扩展方法。
The Join method is like a SQL join, where the list are cross referenced based upon a condition, it isn't a string concatenation or Adding to a list. The Union method does do what you want, as does the Concat method, but both are LAZY evaluations, and have the requirement the parameters be non-null. They return either a ConcatIterator or a UnionIterator, and if called repeatedly this could cause problems. Eager evaluation results in different behavior, if that is what you want, then an extension method like the below could be used.
延迟调用第二个及后续枚举
我通常使用 Linq
IEnumerable.Concat()
但今天我需要 100% 确定第二个枚举未被枚举直到第一个处理完毕为止。 (例如,我不想同时运行两个数据库查询)。所以下面的函数巧妙地延迟了枚举。用法:
在此示例中,GetEnumerable2 函数调用将被延迟,直到 GetEnumerable1 枚举完毕为止。
Delayed invocation of the second and subsequent enumerables
I usually use Linq
IEnumerable<T>.Concat()
but today I needed to be 100% sure that the second enumeration was not enumerated until the first one has been processed until the end. (e.g. two db queries that I didn't want to run simultaneously). So the following function made the trick to delay the enumerations.Usage:
In this example GetEnumerable2 function invocation will be delayed until GetEnumerable1 has been enumerated till the end.