Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
可枚举。包含两个以上的参数c#_C#_String_Concat_Enumerable - Fatal编程技术网

可枚举。包含两个以上的参数c#

可枚举。包含两个以上的参数c#,c#,string,concat,enumerable,C#,String,Concat,Enumerable,我有4个包含字符串的集合,如下所示: 第一:xxxxxxxx 第二名:xx 第三:xxxxxxxx 第四:xx 我想将这4个集合合并为:XXXXXXXXXXXXXXXXX 我想使用Enumerable.Concat,但我有一个问题,它只需要两个参数。因此,我只能将第一个与第二个(例如)结合在一起,但不能将它们全部结合在一起 c#是否提供了一种将两个以上的收藏集中在一起的方法 我是否必须创建两个集合,第一个集合+第二个集合和第三个集合+第四个集合,然后将两个集合合并 编辑 我犯了一个错误,这是我想

我有4个包含字符串的集合,如下所示:

第一:xxxxxxxx 第二名:xx 第三:xxxxxxxx 第四:xx

我想将这4个集合合并为:XXXXXXXXXXXXXXXXX

我想使用Enumerable.Concat,但我有一个问题,它只需要两个参数。因此,我只能将第一个与第二个(例如)结合在一起,但不能将它们全部结合在一起

c#是否提供了一种将两个以上的收藏集中在一起的方法

我是否必须创建两个集合,第一个集合+第二个集合和第三个集合+第四个集合,然后将两个集合合并

编辑


我犯了一个错误,这是我想要展示的4个系列。这些集合包含如前所示的字符串,但这些是集合

您应该使用
string.Concat
方法,
Enumerable.Concat
用于集合

您还可以使用
+
运算符


请注意,字符串是不可变的对象。这意味着当您添加2个字符串时,将创建一个新的字符串对象。因此,出于内存优化的目的,您应该使用
StringBuilder
类来处理动态字符串和5个以上字符串的串联。

如果只有4个字符串,那么有什么问题:

first+ second+ third+ forth;

您可以像这样链接
可枚举.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();

注意:
Enumerable.Concat
将允许重复元素,如果要消除重复元素,可以使用
Enumerable.Union
方法,其余都一样。

如果要连接4个或更少的字符串,则无需自行采取任何特殊操作

首先,请注意

其次,请注意,编译器将把包含四个或更少字符串的串联转换为对适当的
String.Concat()
重载的调用

例如,考虑这个代码:

string a = "a";
string b = "b";
string c = "c";
string d = "d";
string e = a + b + c + d;
连接将转换为以下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 
}
请注意,实际的连接是通过调用
System.String::Concat(String,String,String,String)
完成的

如果连接多个字符串,则可能需要考虑使用<代码> StringBuilder < /代码>,但您可能只会看到超过8个左右字符串的速度提升。



[编辑]确定OP将问题从串联字符串完全更改为串联字符串集合。。。所以这个答案现在毫无意义(

您想要一个长字符串集合,还是一个长字符串?我只想将4个集合合并为1,所以长字符串集合应该是一个好的解决方案。感谢您的回答,您让我意识到我犯了一个错误,这些是我要合并的集合。这些是字符串集合,但它仍然是集合。这就是为什么我要sed Enumerable.concatindecd,但是谢谢你的anwser,它很快就会对我的项目非常有用。谢谢,听起来正是我想要的。我想我不必使用Union,这个Concat只是用来比较Concat值和另一个值,但我会记住它。@Flo添加了做同样事情的替代方法,如果没有用的话否注意到存在的东西是很好的。我已经这样做了,应用您的第一个解决方案:
foreach(viewModel.ActiviteCollection.Where中的var项(c=>c.pmrqtotmativite==Enumerable.Concat(data.Pmid,data.Ligne)。Concat(data.Otm)。Concat(data.Totm))
并且在加法中听起来很有效
SelectMany(x=>x)
,也可以使用聚合(Enumerable.Concat)。
.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 
}