C# 如何将两个 IEnumerable<T> 连接成一个新的 IEnumerable<T>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14164974/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:56:20  来源:igfitidea点击:

How to concatenate two IEnumerable<T> into a new IEnumerable<T>?

c#.netienumerableconcatenation

提问by Samuel Rossille

I have two instances of IEnumerable<T>(with the same T). I want a new instance of IEnumerable<T>which is the concatenation of both.

我有两个IEnumerable<T>(具有相同的T)实例。我想要一个新实例,IEnumerable<T>它是两者的串联。

Is there a build-in method in .Net to do that or do I have to write it myself?

.Net 中是否有内置方法可以做到这一点,还是我必须自己编写?

采纳答案by Jon Skeet

Yes, LINQ to Objects supports this with Enumerable.Concat:

是的,LINQ to Objects 支持Enumerable.Concat

var together = first.Concat(second);

NB: Should firstor secondbe null you would receive a ArgumentNullException. To avoid this & treat nulls as you would an empty set, use the null coalescing operator like so:

注意:应该firstsecond为空你会收到一个ArgumentNullException. 为了避免这种情况并像处理空集一样处理空值,请使用空值合并运算符,如下所示:

var together = (first ?? Enumerable.Empty<string>()).Concat(second ?? Enumerable.Empty<string>()); //amending `<string>` to the appropriate type

回答by Jay Shukla

You can use below code for your solution:-

您可以使用以下代码作为您的解决方案:-

public void Linq94() 
{ 
    int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
    int[] numbersB = { 1, 3, 5, 7, 8 }; 

    var allNumbers = numbersA.Concat(numbersB); 

    Console.WriteLine("All numbers from both arrays:"); 
    foreach (var n in allNumbers) 
    { 
        Console.WriteLine(n); 
    } 
}

回答by Hasse

// The answer that I was looking for when searching
public void Answer()
{
    IEnumerable<YourClass> first = this.GetFirstIEnumerableList();
    // Assign to empty list so we can use later
    IEnumerable<YourClass> second = new List<YourClass>();

    if (IwantToUseSecondList)
    {
        second = this.GetSecondIEnumerableList();  
    }
    IEnumerable<SchemapassgruppData> concatedList = first.Concat(second);
}

回答by supercat

The Concatmethod will return an object which implements IEnumerable<T>by returning an object (call it Cat) whose enumerator will attempt to use the two passed-in enumerable items (call them A and B) in sequence. If the passed-in enumerables represent sequences which will not change during the lifetime of Cat, and which can be read from without side-effects, then Cat may be used directly. Otherwise, it may be a good idea to call ToList()on Catand use the resulting List<T>(which will represent a snapshot of the contents of A and B).

Concat方法将返回一个对象,该对象IEnumerable<T>通过返回一个对象(称为 Cat)来实现,该对象的枚举器将尝试按顺序使用两个传入的可枚举项(称为 A 和 B)。如果传入的可枚举表示的序列在 Cat 的生命周期内不会改变,并且可以无副作用地读取,那么可以直接使用 Cat。否则,它可能是一个好主意,调用ToList()Cat,并使用所得到的List<T>(A和B的内容,这将代表一个快照)。

Some enumerables take a snapshot when enumeration begins, and will return data from that snapshot if the collection is modified during enumeration. If B is such an enumerable, then any change to B which occurs before Cat has reached the end of A will show up in Cat's enumeration, but changes which occur after that will not. Such semantics may likely be confusing; taking a snapshot of Cat can avoid such issues.

一些枚举在枚举开始时拍摄快照,如果在枚举期间修改集合,将从该快照返回数据。如果 B 是这样的可枚举,那么在 Cat 到达 A 末尾之前发生的对 B 的任何更改都将显示在 Cat 的枚举中,但之后发生的更改不会。这种语义可能会令人困惑;拍摄 Cat 快照可以避免此类问题。