我有给定的数据(我只是将其作为List
此处)。
List<string> list1 = new List<string>();
foreach( var x in Regex.Split( "A B C D E F", " " ) )
list1.Add( x );
现在,我要像这样最终清单。
List<string[]> list2 = new List<string[]>();
因此,我尝试了此代码(我尝试了LINQ
但没有收获)。
int i = 0;
string[] array1 = new string[2];
foreach( var x in list1 )
{
if( i % 2 == 0 )
{
array1[0] = x;
}
else
{
array1[1] = x;
list2.Add( array1 );
array1 = new string[2];
}
i++;
}
我想使用LINQ获得相同的结果。请帮忙。
谢谢。
(编辑:对于列表2中的每个项目,结果应为A和B,C和D,E和F)
list2 = list1.Where((x, i) => i % 2 == 0)
.Zip(list1.Where((x, i) => i % 2 == 1),
(x, y) => new[] { x, y })
.ToList();
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句