简体   繁体   English

Enumerable.Concat两个以上的参数C#

[英]Enumerable.Concat more than two parameters c#

I have 4 collections containing strings, like this: 我有4个包含字符串的集合,如下所示:

first: xxxxxxxx second: xx third: xxxxxxxx fourth: xx 第一:xxxxxxxx第二:xx第三:xxxxxxxx第四:xx

I want to concat these 4 collections to get: xxxxxxxxxxxxxxxxxxxx 我想要合并这4个集合以获取:xxxxxxxxxxxxxxxxxxxx

I wanted to use the Enumerable.Concat but I have one problem with this, It only take two parameters. 我想使用Enumerable.Concat,但是我有一个问题,它只需要两个参数。 As a consequence I can concat only the first with the second (for example) but not all of'em together. 结果,我只能将第一个与第二个(例如)连接在一起,而不能将它们全部连接在一起。

Is c# offers a way to concat more than 2 collections together? c#是否提供一种将两个以上的集合连接在一起的方法?

Am I having to create two collections which concat first + second and third + fourth and then concat the two collections together? 我是否必须创建两个集合,它们先合并第一个+第二个和第三个+第四个,然后再合并两个集合?

EDIT 编辑

I made a mistake, It's 4 collections I want to concat. 我弄错了,这是我要收藏的4个收藏。 These collections contains strings like shown before, but these are Collections 这些集合包含前面显示的字符串,但这是集合

You can chain the Enumerable.Concat calls like this. 您可以像这样链接Enumerable.Concat调用。

List<string> list1 = new List<string>();
List<string> list2 = new List<string>();
List<string> list3 = new List<string>();
List<string> list4 = new List<string>();

//Populate the lists

var mergedList = list1.Concat(list2)
    .Concat(list3)
    .Concat(list4)
    .ToList();

Another option is to create an array and call SelectMany 另一个选择是创建一个数组并调用SelectMany

var mergedList = new[]
{
    list1, list2, list3, list4
}
.SelectMany(x => x)
.ToList();

Note: Enumerable.Concat will allow duplicate elements, if you want to eliminate duplicates you can use Enumerable.Union method, rest all same. 注意: Enumerable.Concat将允许重复的元素,如果要消除重复的元素,则可以使用Enumerable.Union方法,其余都Enumerable.Union

You should use string.Concat method, Enumerable.Concat is meant for collections. 您应该使用string.Concat方法, Enumerable.Concat用于集合。

You could also use + operator. 您也可以使用+运算符。

Please note, that strings are immutable objects. 请注意,字符串是不可变的对象。 It means that when you add 2 string, a new string object is being created. 这意味着当您添加2个字符串时,将创建一个新的字符串对象。 Therefore, for memory optimization purposes, you should use StringBuilder class for dynamic strings and concatenation of more than 5 strings. 因此,出于内存优化的目的,应将StringBuilder类用于动态字符串和5个以上字符串的串联。

If it's only four strings, what's wrong with: 如果只有四个字符串,那怎么了:

first+ second+ third+ forth;

??? ???

You don't need to take any special action yourself if you are concatenating 4 strings or less. 如果连接的字符串少于或等于4个,则无需自己采取任何特殊操作。

Firstly, note that there are overloads of String.Concat() taking between one and four string parameters . 首先,请注意, String.Concat()重载采用一到四个string参数

Secondly, note that the compiler will translate concatenations involving four or less strings into calls to the appropriate String.Concat() overload. 其次,请注意,编译器会将涉及四个或更少字符串的串联转换为对适当String.Concat()重载的调用。

For example, consider this code: 例如,考虑以下代码:

string a = "a";
string b = "b";
string c = "c";
string d = "d";
string e = a + b + c + d;

The concatenation is turned into this IL: 串联变为以下IL:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 4
    .locals init (
        [0] string a,
        [1] string b,
        [2] string c,
        [3] string d,
        [4] string e)
    L_0000: ldstr "a"
    L_0005: stloc.0 
    L_0006: ldstr "b"
    L_000b: stloc.1 
    L_000c: ldstr "c"
    L_0011: stloc.2 
    L_0012: ldstr "d"
    L_0017: stloc.3 
    L_0018: ldloc.0 
    L_0019: ldloc.1 
    L_001a: ldloc.2 
    L_001b: ldloc.3 
    L_001c: call string [mscorlib]System.String::Concat(string, string, string, string)
    L_0021: stloc.s e
    L_0023: ldloc.s e
    L_0025: call void [mscorlib]System.Console::WriteLine(string)
    L_002a: ret 
}

Note that the actual concatenation is done by a call to System.String::Concat(string, string, string, string) . 请注意,实际的串联是通过调用System.String::Concat(string, string, string, string)

If you are concatenating more strings, you might want to consider using StringBuilder instead, but you will probably only see a speed improvement for more than 8 or so strings. 如果要串联更多的字符串,则可能要考虑使用StringBuilder ,但是您可能只会看到速度提高了大约8个以上的字符串。


[EDIT] OK the OP completely changed the question from concatenating strings to concatenating collections of strings... so this answer is now pointless. [编辑]好吧,OP完全将问题从串联字符串更改为串联字符串集合...因此,这个答案现在毫无意义。 :( :(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM