C# Concat列表中的所有字符串<;列表<;字符串>&燃气轮机;使用LINQ
我的问题与此几乎相同,但列表维度是n。C# Concat列表中的所有字符串<;列表<;字符串>&燃气轮机;使用LINQ,c#,.net,linq,C#,.net,Linq,我的问题与此几乎相同,但列表维度是n。 如何在列表中包含所有字符串,因为链接的问题被标记为c,所以我用c代码添加了这个答案 如果已知嵌套列表的数量,则必须反复使用SelectMany()将所有嵌套列表展开为字符序列。然后用那个序列做一个字符串 List<List<List<string>>> nestedList = new List<List<List<string>>>(); var result = new strin
如何在
列表中包含所有字符串,因为链接的问题被标记为c,所以我用c代码添加了这个答案
如果已知嵌套列表的数量,则必须反复使用SelectMany()
将所有嵌套列表展开为字符序列。然后用那个序列做一个字符串
List<List<List<string>>> nestedList = new List<List<List<string>>>();
var result = new string(nestedList.SelectMany(x => x).SelectMany(x => x).SelectMany(x => x).ToArray());
更新:
在做了一点修改之后,我最终完成了这个实现。也许不试抓更好
private static string ConcatAll<T>(T nestedList) where T : IList
{
dynamic templist = nestedList;
while (templist.Count > 0 && !(templist[0] is char?))
{
List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x =>
{
var s = x as string;
if (s != null)
{
return s.Cast<dynamic>();
}
return x;
}).ToList();
templist = inner;
}
return new string(((List<object>) templist).Cast<char>().ToArray());
}
私有静态字符串ConcatAll(T nestedList),其中T:IList
{
动态模板列表=嵌套列表;
while(templast.Count>0&&!(templast[0]是char?)
{
列表内部=新列表(模板列表)。选择多个(x=>
{
var s=x作为字符串;
如果(s!=null)
{
返回s.Cast();
}
返回x;
}).ToList();
圣殿骑士=内心;
}
返回新字符串(((列表)templast.Cast().ToArray());
}
另一种解决方案是使用递归方法展平所有列表:
static IEnumerable<string> Flatten(IEnumerable enumerable)
{
foreach (object el in enumerable)
{
if (enumerable is IEnumerable<string>)
{
yield return (string) el;
}
else
{
IEnumerable candidate = el as IEnumerable;
if (candidate != null)
{
foreach (string nested in Flatten(candidate))
{
yield return nested;
}
}
}
}
}
静态IEnumerable展平(IEnumerable enumerable)
{
foreach(可枚举中的对象el)
{
if(可枚举为IEnumerable)
{
收益率返回(字符串)el;
}
其他的
{
IEnumerable候选者=el为IEnumerable;
if(候选者!=null)
{
foreach(嵌套在展平中的字符串(候选))
{
收益率嵌套;
}
}
}
}
}
使用此方法,您可以通过以下方式压缩所有字符串:
List<List<List<string>>> nestedList = new List<List<List<string>>>
{
new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
};
Console.WriteLine(String.Join(" ",Flatten(nestedList)));
List nestedList=新列表
{
新列表{新列表{“你好”},新列表{“世界”},
新列表{新列表{“再见”},新列表{“世界”,“结束”}
};
Console.WriteLine(String.Join(“,flant(nestedList));
这个想法就是从这里来的。用c#对吧?因为同一个问题被标记为c#。由于列表的类型未知,您应该使用反射进行此操作。请使用。选择many
将其展平,然后使用找到的答案。使用。因为它不使用反射。它比我的方法快一百倍。所以如果我是你,我会接受他的回答;)回答得很好。我正要发布类似的内容,但无法使其正常工作:-(。顺便问一下,是否需要空签入else
子句?如果需要,请解释原因?
private static string ConcatAll<T>(T nestedList) where T : IList
{
dynamic templist = nestedList;
while (templist.Count > 0 && !(templist[0] is char?))
{
List<dynamic> inner = new List<dynamic>(templist).SelectMany<dynamic, dynamic>(x =>
{
var s = x as string;
if (s != null)
{
return s.Cast<dynamic>();
}
return x;
}).ToList();
templist = inner;
}
return new string(((List<object>) templist).Cast<char>().ToArray());
}
static IEnumerable<string> Flatten(IEnumerable enumerable)
{
foreach (object el in enumerable)
{
if (enumerable is IEnumerable<string>)
{
yield return (string) el;
}
else
{
IEnumerable candidate = el as IEnumerable;
if (candidate != null)
{
foreach (string nested in Flatten(candidate))
{
yield return nested;
}
}
}
}
}
List<List<List<string>>> nestedList = new List<List<List<string>>>
{
new List<List<string>> {new List<string> {"Hello "}, new List<string> {"World "}},
new List<List<string>> {new List<string> {"Goodbye "}, new List<string> {"World ", "End "}}
};
Console.WriteLine(String.Join(" ",Flatten(nestedList)));